C# 按钮单击事件中if条件的Xunit测试
我已经搜索了一段时间,但没有找到合适的答案。 我有一个按钮单击事件,它使用C# 按钮单击事件中if条件的Xunit测试,c#,unit-testing,xunit,C#,Unit Testing,Xunit,我已经搜索了一段时间,但没有找到合适的答案。 我有一个按钮单击事件,它使用if语句比较两个datetimes public void SaveButton_Click(object sender, EventArgs e) { //Parameters for SQL command if (AnticipatedDatePicker.Value < OutDatePicker.Value) { Message
if
语句比较两个datetimes
public void SaveButton_Click(object sender, EventArgs e)
{
//Parameters for SQL command
if (AnticipatedDatePicker.Value < OutDatePicker.Value)
{
MessageBox.Show("Error. The Anticipated Check In date must be after the Check Out date");
}
if (OutDatePicker.Value < DateTime.Today)
{
MessageBox.Show("Error. The Check Out Date must not be before today.");
}
//else statement here
}
它确实通过了。但是,它并没有真正触动SaveButton\u click事件,也没有触动它使用的值。
如果我有办法将第一个
和第二个
值传递给ExpectedDatePicker.Value
和OutDatePicker.Value
并测试实际按钮(单击事件)以及If
语句是否被触发?
任何帮助都将不胜感激!
谢谢 首先,我建议在这里使用MVVM模式,所以您应该使用绑定、命令并摆脱按钮点击处理程序 另一个问题是日期时间。今天,你需要找到模仿的方法。。。也许你可以创建一些服务来包装它。否则,我看不到创建正确测试的方法
[Theory]
[InlineData("2020-12-30", "2020-12-31")]
public void checkDates(DateTime first, DateTime second)
{
Assert.True(first < second);
Assert.False(first == second);
Assert.False(first < DateTime.Today);
}