2013-01-16 63 views
0

所以,我用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()

回答

0

我不得不承认,我不熟悉的MVC验证所以这可能会或可能无法正常工作。

不过,我猜想,如果你单独使用NUnit,你可能想要做这样的事情

[FixtureSetup] 
public void ....() 
{ 
    DataAnnotationsViewModelValidatorProvider.RegisterAdapter(..., ...); 
} 

而此刻的你实际上是通过绑定,这是一个整体加入您的验证反射一跳。

但是,如果你在自动生成的xxxxx.feature.cs看文件,你可以看到类实际上定义为

[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.8.1.0")] 
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 
[NUnit.Framework.TestFixtureAttribute()] 
[NUnit.Framework.DescriptionAttribute("xxxxx")] 
public partial class xxxxxFeature 
{ 

显然,我们不能编辑这一点,但我们可以创建另一个文件在部分类中实现我们喜欢的任何东西。

在xxxxx.partial.cs

public partial class xxxxxFeature 
{ 
    [FeatureSetup] 
    .... 

如果不出意外,你有几个地方试试。祝你好运。

+0

感谢您的建议!现在,我决定为我的'RequiredIf.IsValid()'实现增加更多的责任,不再需要注册。感谢您考虑答案 - 即使我们不能拿出一个:( –