Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按钮单击事件中if条件的Xunit测试_C#_Unit Testing_Xunit - Fatal编程技术网

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);

        }