reckface
reckface

Reputation: 5858

How can I unit test Nancy modules with F#?

I'm trying to test Nancy modules with F# as described here, the thing is I can't see how to pass the second parameter in F#.

Here's what I have so far:

let should_return_status_ok_for_get() =
    let bootstrapper = new DefaultNancyBootstrapper()    
    let browser = new Browser(bootstrapper, fun req -> req.Accept(new Responses.Negotiation.MediaRange("application/json")))    
    let result = browser.Get("/Menu", fun req ->  req.HttpRequest())
    Assert.AreEqual (HttpStatusCode.OK, result.StatusCode)
    result

in the example, I should be able to instantiate a Browser object to test a specific Module:

var browser = new Browser(with => with.Module(new MySimpleModule()));

But I get a compile time error in F# when I try:

let browser = new Browser(fun req -> req.Module(new MenuModule()))

EDIT Error: No overloads match for method 'Browser'

Are there any examples of this in F#? Also, is this the best way to go about this in F#?

Upvotes: 2

Views: 189

Answers (1)

dustinmoris
dustinmoris

Reputation: 3361

This is how I run Nancy tests in F#:

I create a new bootstrapper in my test project by deriving from the DefaultNancyBootstrapper. I use this bootstrapper to register my mocks:

type Bootstrapper() =
    inherit DefaultNancyBootstrapper()

    override this.ConfigureApplicationContainer(container : TinyIoCContainer) =
        base.ConfigureApplicationContainer(container)    
        container.Register<IMyClass, MyMockClass>() |> ignore

Then I write a simple test method to execute a GET request like so:

[<TestFixture>]
type ``Health Check Tests`` () =    

    [<Test>]
    member test.``Given the service is healthy the health check endpoint returns a HTTP 200 response with status message "Everything is OK"`` () =
        let bootstrapper = new Bootstrapper()
        let browser = new Browser(bootstrapper)

        let result = browser.Get("/healthcheck")
        let healthCheckResponse = JsonSerializer.deserialize<HealthCheckResponse> <| result.Body.AsString()

        result.StatusCode            |> should equal HttpStatusCode.OK
        healthCheckResponse.Message  |> should equal "Everything is OK"

Let me know if this helps!

Upvotes: 1

Related Questions