所以,我用MVC应用程序,我的团队使用SpecFlow测试工作,以验证器的属性。我使用here描述的[RequiredIf(prop, val)]
的实现。约束力的SpecFlow单元测试
但是,我发现了一个“轻微”的问题 - 而验证工作只是正常的网页,他们在我们的单元测试突破!经过调查,我发现该属性的IsValid()方法在我们的单元测试中被直接调用......可能是因为该属性未绑定到Validator。
在那个博客,我也跟着安装步骤,注册验证程序RequiredIf属性。但是,为了某些单元测试的目的,我需要找出在测试设置中绑定验证的位置。
我已经尝试了一些更多或更少的逻辑选择:
[Binding]
public class TestSteps
{
// Every test has to call this helper to load up the controller...
private void GoToHome()
{
// SNIP: Unimportant
DataAnnotationsViewModelValidatorProvider.RegisterAdapter(..., ...);
}
}
...以及在测试套件文件...
// See attribute for why I figured this may be a logical choice.
[BeforeScenario]
public void Setup()
{
DataAnnotationsViewModelValidatorProvider.RegisterAdapter(..., ...);
}
...然而,出于某种原因,无论是位置导致RequiredIf()
绑定到其RequiredIfValidator()
。
问题:对于单元测试,我应该在哪里放置属性 - >验证器绑定,这样我的单元测试将正确验证装饰的属性,如果RequiredIf()
?
感谢您的建议!现在,我决定为我的'RequiredIf.IsValid()'实现增加更多的责任,不再需要注册。感谢您考虑答案 - 即使我们不能拿出一个:( –