我正在尝试使用F#测试Nancy模块,如here所述,事情是我无法看到如何在F#中传递第二个参数。
这是我到目前为止所拥有的:
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
在示例中,我应该能够实例化一个Browser对象来测试一个特定的模块:
var browser = new Browser(with => with.Module(new MySimpleModule()));
但是当我尝试时,我在F#中遇到编译时错误:
let browser = new Browser(fun req -> req.Module(new MenuModule()))
编辑错误:方法“浏览器”
没有重载匹配F#中有这方面的例子吗? 另外,这是在F#中解决这个问题的最好方法吗?
答案 0 :(得分:1)
这是我在F#中运行Nancy测试的方式:
我通过派生DefaultNancyBootstrapper
在我的测试项目中创建了一个新的引导程序。我使用这个引导程序来注册我的模拟:
type Bootstrapper() =
inherit DefaultNancyBootstrapper()
override this.ConfigureApplicationContainer(container : TinyIoCContainer) =
base.ConfigureApplicationContainer(container)
container.Register<IMyClass, MyMockClass>() |> ignore
然后我编写一个简单的测试方法来执行GET请求,如下所示:
[<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"
请告诉我这是否有帮助!