F# 如何为Saturn应用程序构建集成测试
我正在尝试为我使用构建的小型API构建一些集成测试 该API使用常见的F# 如何为Saturn应用程序构建集成测试,f#,saturn-framework,F#,Saturn Framework,我正在尝试为我使用构建的小型API构建一些集成测试 该API使用常见的Saturn计算表达式构建,如应用程序、控制器、路由器等 但是为了构建集成测试,我需要替换应用程序计算表达式(ce)和手工制作WebHostBuilder 我的应用程序ce如下所示: module Server let app = application { url ("http://0.0.0.0:" + port.ToString() + "/") use_router apiR
Saturn
计算表达式构建,如应用程序
、控制器
、路由器
等
但是为了构建集成测试,我需要替换应用程序
计算表达式(ce)和手工制作WebHostBuilder
我的应用程序
ce如下所示:
module Server
let app =
application {
url ("http://0.0.0.0:" + port.ToString() + "/")
use_router apiRouter
memory_cache
service_config configureSerialization
use_gzip
use_config (fun _ ->
System.Environment.CurrentDirectory <- (System.Reflection.Assembly.GetExecutingAssembly()).Location
|> Path.GetDirectoryName
let configurationRoot =
ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json")
.Build()
let appConfig = FsConfig.AppConfig(configurationRoot)
let dbPath =
match appConfig.Get<AppSettings>() with
| Ok settings when settings.DbConfig.Database.Contains(":memory:") -> settings.DbConfig.Database
| Ok settings -> Path.Combine(System.Environment.CurrentDirectory, settings.DbConfig.Database)
| Error _ -> failwithf "Invalid database path"
{ connectionString = dbPath |> sprintf "DataSource=%s;Version=3" })
}
上面的代码在我的API项目中,下面带有集成测试的代码在第二个项目中。后者的项目参考了前者
let configureApp (app : IApplicationBuilder) =
app.UseGiraffe(Server.apiRouter) \\<-- The problem is here. Server.apiRouter is null!
let configureServices (services : IServiceCollection) =
services.AddGiraffe() |> ignore
let builder = WebHostBuilder()
.UseContentRoot(contentRoot)
.Configure(Action<IApplicationBuilder> configureApp)
.ConfigureServices(configureServices)
let testServer = new TestServer(builder)
let client = testServer.CreateClient()
let! response = client.GetAsync "/"
test <@ HttpStatusCode.OK = response.StatusCode @>
问题似乎出在app.usegirafe(Server.apiRouter)行上<代码>apiRouter是在API项目的服务器
模块中定义的-但是当此代码在测试项目服务器中运行时。apiRouter
为空
但是,如果我将测试代码移动到与API
相同的项目中,那么测试就可以完美地工作
如果从测试项目调用,
apiRouter
计算表达式为什么会为null
事实证明,这与土星毫无关系。这一切都与F#
启动模块的方式有关
正如参考文章中所建议的那样。我只是简单地添加了一个[]
函数,一切都按预期工作
let configureApp (app : IApplicationBuilder) =
app.UseGiraffe(Server.apiRouter) \\<-- The problem is here. Server.apiRouter is null!
let configureServices (services : IServiceCollection) =
services.AddGiraffe() |> ignore
let builder = WebHostBuilder()
.UseContentRoot(contentRoot)
.Configure(Action<IApplicationBuilder> configureApp)
.ConfigureServices(configureServices)
let testServer = new TestServer(builder)
let client = testServer.CreateClient()
let! response = client.GetAsync "/"
test <@ HttpStatusCode.OK = response.StatusCode @>
System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'A suitable constructor for type 'Giraffe.Middleware+GiraffeMiddleware' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.'