UI 방법이나 관련 방법을 게시하면 해결 방법을 훨씬 쉽게 제공 할 수 있습니다. 또한 TestMethod를 보면 불완전한 메소드까지도 도움이 될 수 있습니다.
테스트 목적이 다른 클릭 가능성에서 어떤 결과가 발생하는지 판단하는 것이라면 어떻게해야합니까? 이렇게 될 것이다 (위쪽의 using Microsoft.VisualStudio.TestTools.UnitTesting;
를 포함하는 기억)
public class ClassUnderTest
{
private static Func<string, string, MessageBoxButtons, DialogResult>
_messageBoxLocator = MessageBox.Show;
public static Func<string, string, MessageBoxButtons, DialogResult>
MessageBoxDependency
{
get { return _messageBoxLocator; }
set { _messageBoxLocator = value; }
}
private void MyMethodOld(object sender, EventArgs e)
{
if (MessageBox.Show("test", "", MessageBoxButtons.YesNo) ==
System.Windows.Forms.DialogResult.Yes)
{
//Yes code
AnsweredYes = true;
}
else
{
//No code
}
}
public bool AnsweredYes = false;
public void MyMethod(object sender, EventArgs e)
{
if (MessageBoxDependency(
"testText", "testCaption", MessageBoxButtons.YesNo)
==
System.Windows.Forms.DialogResult.Yes)
{
//proceed code
AnsweredYes = true;
}
else
{
//abort code
}
}
}
다음 시험 방법을 :
이 같은 Inversion of Control 및 Dependency Injection를 사용하여 MessageBox
을 트리거 실제 방법을 설정할 수 있습니다
[TestMethod]
public void ClassUnderTest_DefaultAnsweredYes_IsFalse()
{
var classUnderTest = new ClassUnderTest();
Assert.AreEqual(false, classUnderTest.AnsweredYes);
}
[TestMethod]
public void MyMethod_UserAnswersYes_AnsweredYesIsTrue()
{
//Test Setup
Func<string, string, MessageBoxButtons, DialogResult>
fakeMessageBoxfunction =
(text, caption, buttons) =>
DialogResult.Yes;
//Create an instance of the class you are testing
var classUnderTest = new Testing.ClassUnderTest();
var oldDependency = Testing.ClassUnderTest.MessageBoxDependency;
Testing.ClassUnderTest.MessageBoxDependency = fakeMessageBoxfunction;
try
{
classUnderTest.MyMethod(null, null);
Assert.AreEqual(true, classUnderTest.AnsweredYes);
//Assert What are you trying to test?
}
finally
{ //Ensure that future tests are in the default state
Testing.ClassUnderTest.MessageBoxDependency = oldDependency;
}
}
그렇다면이 컨트롤을 클릭 할 때 컨트롤이 수행하는 작업을 테스트하고 싶습니까? 또는 테스트 자체가 중간 계층의 테스트가 안전한지 확인하기위한 차단 메시지 상자 대화 상자가 있습니까? – Maslow