Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 如何为Saturn应用程序构建集成测试_F#_Saturn Framework - Fatal编程技术网

F# 如何为Saturn应用程序构建集成测试

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

我正在尝试为我使用构建的小型API构建一些集成测试

该API使用常见的
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.'