测试NancyFX模型验证

时间:2015-11-22 10:50:07

标签: validation testing nancy

上午,

我在NancyFX(1.4.1)中测试模型验证。点击API端点时验证工作正常,但在测试中失败。我使用的是默认的bootstrapper。测试是在一个单独的项目中。这两个项目都引用了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));
}

1 个答案:

答案 0 :(得分:0)

如演示代码所示,CreateServiceCommand上的数据注释不起作用。

我必须实现

的实例
AbstractValidator<CreateServiceCommand>
来自FluentValidation的