2 回答
TA贡献1836条经验 获得超4个赞
您可以使用以下内容(这使用 xunit,根据您的首选框架进行调整)
public class AddressValidationShould
{
private AddressValidator Validator {get;}
public AddressValidationShould()
{
Validator = new AddressValidator();
}
[Fact]
public void NotAllowEmptyPostcode()
{
var address = new Address(); // You should create a valid address object here
address.Postcode = string.empty; // and then invalidate the specific things you want to test
Validator.Validate(address).IsValid.Should().BeFalse();
}
}
...显然创建其他测试来涵盖应该/不应该允许的其他事情。比如AddressLine140以上无效,40以下有效。
TA贡献1804条经验 获得超2个赞
使用 MSTest,您可以编写
[TestMethod]
public void NotAllowEmptyPostcode()
{
// Given
var address = new Address(); // You should create a valid address object here
address.Postcode = string.empty; // invalidate the specific property
// When
var result = validator.Validate(address);
// Then (Assertions)
Assert.That(result.Errors.Any(o => o.PropertyName== "Postcode"));
}
- 2 回答
- 0 关注
- 196 浏览
添加回答
举报