【发布时间】:2026-01-17 21:35:01
【问题描述】:
所以,我正在使用我的团队正在使用 SpecFlow 测试的 MVC 应用程序。我使用[RequiredIf(prop, val)] 的实现,描述为here。
但是,我发现了一个“轻微”问题 - 虽然验证在网页上运行良好,但它们会破坏我们的单元测试!经过调查,我发现在我们的单元测试中直接调用了属性的 IsValid() 方法……可能是因为属性没有绑定到验证器。
在那个博客上,我按照设置步骤向验证器注册了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()。
问题:对于单元测试,我应该将 Attribute -> Validator 绑定放在哪里,这样我的单元测试才能正确验证在 RequiredIf() 时修饰的属性?
【问题讨论】:
标签: c# asp.net-mvc-3 unit-testing validation specflow