如何使用 F# 对 Nancy 模块进行单元测试?
How can I unit test Nancy modules with F#?
我正尝试按照 here 所述使用 F# 测试 Nancy 模块,问题是我看不到如何在 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
在示例中,我应该能够实例化一个浏览器对象来测试特定的模块:
var browser = new Browser(with => with.Module(new MySimpleModule()));
但是当我尝试时在 F# 中遇到编译时错误:
let browser = new Browser(fun req -> req.Module(new MenuModule()))
EDIT 错误:没有与方法 'Browser'
匹配的重载
F# 中有这方面的示例吗?
另外,这是在 F# 中解决此问题的最佳方法吗?
这就是我 运行 Nancy 在 F# 中测试的方式:
我通过派生自 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"
如果有帮助请告诉我!
我正尝试按照 here 所述使用 F# 测试 Nancy 模块,问题是我看不到如何在 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
在示例中,我应该能够实例化一个浏览器对象来测试特定的模块:
var browser = new Browser(with => with.Module(new MySimpleModule()));
但是当我尝试时在 F# 中遇到编译时错误:
let browser = new Browser(fun req -> req.Module(new MenuModule()))
EDIT 错误:没有与方法 'Browser'
匹配的重载F# 中有这方面的示例吗? 另外,这是在 F# 中解决此问题的最佳方法吗?
这就是我 运行 Nancy 在 F# 中测试的方式:
我通过派生自 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"
如果有帮助请告诉我!