C# NUnit检查按钮是否已单击
我正在学习如何进行单元测试。我试图搜索如何对按钮点击事件进行测试,但找不到我能理解的答案。 如何检查是否引发了C# NUnit检查按钮是否已单击,c#,nunit,C#,Nunit,我正在学习如何进行单元测试。我试图搜索如何对按钮点击事件进行测试,但找不到我能理解的答案。 如何检查是否引发了TextBlock\u MouseDown事件,然后是来自PostMethod的响应,该方法不能为null。我的问题是,我找不到方法或不知道如何使用NUnit检查TextBlock\u MouseDown事件 public partial class MainWindow : Window { public MainWindow(){ InitializeComp
TextBlock\u MouseDown
事件,然后是来自PostMethod
的响应,该方法不能为null
。我的问题是,我找不到方法或不知道如何使用NUnit
检查TextBlock\u MouseDown
事件
public partial class MainWindow : Window
{
public MainWindow(){
InitializeComponent();
}
public void TextBlock_MouseDown(object sender, MouseButtonEventArgs e){
//Check return value from PostMethod
string reponse = PostMethod("myurlhere");
}
public static String PostMethod(String val){
//Code
}
}
[TestFixture]
public class TestClass
{
[Test]
public void PostMethod_IsAlive_returnHello()
{
//ARRANGE
String url = "myurlhere";
//ACT
string response = MainWindow.PostMethod(url);
//ASSERT
Assert.AreEqual("\"Hello\"", response);
}
}
如果要测试
UI
,应该使用测试库进行UI测试。
例如FlaUI
()
NUnit
用于测试代码隐藏。例如,如果要检查PostMethod()
是否返回特定值
如果您只想检查
PostMethod()
的结果,请将方法外包给另一个类
,并对其进行测试(MainWindow现在将在其代码隐藏中使用OutsourcedClass.PostMethod()
)
外包班
单元测试
所以在单元测试中模拟点击事件是不可能的?这就是它的工作原理吗?我不是在测试我的ui外观。我正在测试我的后端。NUnit
仅测试代码隐藏,如果要测试ui按钮,必须使用FlaUI
。如果此答案对您有帮助,请将其标记为答案
public class OutsourcedClass
{
public string PostMethod(string url)
{
return url;
}
}
public class OutsourcedClassTest
{
private OutsourcedClass _Instance;
[SetUp]
public void Setup()
{
_Instance = new OutsourcedClass();
}
[Test]
public void PostMethodTest()
{
string url = "foo";
Assert.AreEqual(url, _Instance.PostMethod(url));
}
}