迁移到ASP.NET Core RC2后,集成测试中断

Mar*_*tin 7 c# asp.net integration-testing asp.net-core

在我的集成测试中,我使用一个TestServer类来为我的集成测试工作一个测试服务器实例.在RC1中,我使用以下代码实例化它:

var server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
Run Code Online (Sandbox Code Playgroud)

在RC2上,TestServer.CreateBuilder()已被删除.因此,我尝试使用以下代码创建一个新的TestServer:

var server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
Run Code Online (Sandbox Code Playgroud)

我面临的问题是在RC2之后,运行时无法解析DI的依赖关系,因此它会在Startup类的Configure方法上抛出异常.但是,如果我启动实际服务器(而不是测试项目),系统会启动.抛出的异常如下:

  System.Exception : Could not resolve a service of type 'ShikashiBot.IShikashiBotManager' for the parameter 'botManager' of method 'Configure' on type 'ShikashiBot.Startup'.
Run Code Online (Sandbox Code Playgroud)

我目前正在为测试主机使用以下软件包: Microsoft.AspNetCore.TestHost": "1.0.0-rc2-final

Mar*_*cze 7

我需要进行一些更改才能让你的回购工作:

  1. 我不得不重新命名appsettings.sample.jsonappsettings.json,我想这仅仅是因为它不是在源代码控制.
  2. 我不得不添加"buildOptions": { "copyToOutput": [ "appsettings.json" ] }project.json该IntegrationTests项目.
  3. 只好日志级别更改VerboseDebugappsettings.json.

但在此之后,集成测试EndPointsRequiresAuthorization经过了依赖注入,而对我来说,在失败的异常ShikashiBotManager,我猜是因为我没有Postgre DB成立做.
对于你来说,它之前已经失败了,因为它无法解析IShikashiBotManager界面,对吧?

您是否可以尝试使用git clean -xfd(注意:您的未提交的本地更改将被删除)完成对本地存储库的清除,重建并重试?