dimanche 22 novembre 2015

Testing NancyFX Model Validation

Morning,

I'm testing model validation in NancyFX (1.4.1). The validation works fine when hitting the API endpoint, but fails in the tests. I'm using the default bootstrapper. Is there some more configuration required in the Test browser?

Thanks in advance!

Model:

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

Module under test:

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 "";
        };
    }
}

Test snippet:

[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));
}

Aucun commentaire:

Enregistrer un commentaire