我在 VS2017 中有一个 ASP.NET 应用程序(我捕获一些输入的基本形式)。
表单上的字段之一是 Mark,它是一个整数。
我的 .cs 文件中有以下标记的代码块。
[Display(Name = "Mark")]
[Range(1, 10, ErrorMessage = "Mark must be between 1 and 10")]
public int Mark{ get; set; }
我为此应用程序创建了一个 MSTest 项目来编写单元测试。
我的问题是,您是否为此块编写测试用例以验证输入值是否在预期范围内?
如果是,你是怎么写的?
我已经从这个开始了。
[DataRow(0, "Mark must be between 1 and 10")]
[DataRow(11, "Mark must be between 1 and 10")]
[DataTestMethod]
public void TestMark_IsMarkValid_NotValid(int mark, string expectedMsg)
{
//Arrange
Student testStudent = new Student();
testStudent.Mark = mark; //this does not throw any error, although the assigned value is outside of the defined range. But it looks like the range validation only applies to the webform.
//Act
string actualMsg = "Mark must be between 1 and 10"; //this is not correct. I was thinking to capture in the actual result the error message yield by the Range validation, but assigning a value outside range doesn't yield any error message.
//Assert
Assert.AreEqual(expectedMsg, actualMsg);
}
现在,不确定该块是否应该在单元测试的范围内。如果应该是,我觉得我采取的方法是不正确的。
请问有什么想法吗?
非常感谢,科斯敏