在我的集成测试中,我使用 TestServer
类来为我的集成测试创建一个测试服务器实例。在 RC1 中,我使用以下代码实例化了它:
var server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
在 RC2 上,删除了 TestServer.CreateBuilder()。因此,我尝试使用以下代码创建一个新的 TestServer:
var server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
我面临的问题是,在 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'.
我目前正在为测试主机使用以下包:Microsoft.AspNetCore.TestHost": "1.0.0-rc2-final
最佳答案
我需要一些更改才能使您的存储库正常工作:
- 我不得不将
appsettings.sample.json
重命名为appsettings.json
,我想这只是因为它不在源代码管理中。 - 我必须将
"buildOptions": { "copyToOutput": [ "appsettings.json"] }
添加到 IntegrationTests 项目的project.json
。< - 必须在
appsettings.json
中将日志级别Verbose
更改为Debug
。
但是在这之后集成测试 EndPointsRequiresAuthorization
经历了依赖注入(inject),对我来说它失败了,在 ShikashiBotManager
中出现异常,我猜是因为我没有Postgre 数据库设置。
对你来说,在此之前它已经失败了,因为它无法解析 IShikashiBotManager
接口(interface),对吗?
您可以尝试使用 git clean -xfd
完全清除您的本地存储库吗(注意:您未提交的本地更改将被删除),重建并尝试又是?
关于c# - 迁移到 ASP.NET Core RC2 后集成测试中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37361775/