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 關注
- 241 瀏覽
添加回答
舉報