2015-11-22 121 views
0

上午,測試NancyFX模型驗證

我測試在NancyFX模型驗證(1.4.1)。點擊API端點時驗證工作正常,但在測試中失敗。我正在使用默認引導程序。測試在一個單獨的項目中。兩個項目都引用了nancy.validation.fluentvalidation包。測試瀏覽器中是否需要更多配置?

在此先感謝!

型號:

public class CreateServiceCommand 
{ 
    [Required(AllowEmptyStrings = false)] 
    public string TestField { get; set; } 
} 

下測試模塊:

public class ServiceModule : NancyModule 
{ 
    private readonly IServiceCreateRequestedListener _listener; 

    public ServiceModule(IServiceCreateRequestedListener listener) 
    { 
     _listener = listener; 
     Post["/services/create"] = parameters => 
     { 
      var request = this.Bind<CreateServiceCommand>(); 

      var result = this.Validate(request); 

      if(!result.IsValid) return HttpStatusCode.BadRequest; 

      _listener.CreateServiceRequested(request); 
      return ""; 
     }; 
    } 
} 

試驗片段:

[SetUp] 
public void Setup() 
{ 
    var browser = new Browser(with => 
    { 
     with.Module<ServiceModule>(); 
     with.Dependency<IServiceCreateRequestedListener>(this); 
    }); 

    _result = browser.Post("/services/create", with => 
    { 
     with.HttpRequest(); 
    }); 
} 

[Test] 
public void ShouldReturnBadRequest 
{ 
    Assert.That(_result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); 
} 

回答

0

在CreateServiceCommand數據註釋如圖演示代碼沒有工作。

我不得不FluentValidation

實施

AbstractValidator<CreateServiceCommand> 

實例