且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET Core 3.1中使用简单注入器进行依赖项交换以进行集成测试

更新时间:2023-02-14 14:55:50

进一步挖掘之后,我发现了阻止我进行所有尝试的问题:为了使集成测试注册控制器,您必须更改测试项目SDK来自:

After further digging, I found the issue that prevented all my attempts at working: In order to have your integration test register the controllers you have to change the test project SDK from:

<Project Sdk="Microsoft.NET.Sdk">

收件人:

<Project Sdk="Microsoft.NET.Sdk.Web">

很公平,它在先决条件,但老实说,我认为测试项目模板未附带已配置的模板,或者没有针对该模板的特定模板就不足为奇了这种(非常常见的)场景,或者我发现的所有指南都没有提到这一关键的配置.如果您信任模板,就很容易错过.

And fair enough, it does say so under the prerequisites, but to be honest I think it's beyond strange that the test project templates don't come with this already configured, or that there isn't a specific template for this - very common - scenario, or that none of the guides I've found mention this critical piece of configuration. It's very easy to miss if you trust in the templates.

修复此问题后,现在可以很简单地使用Startup的子类,并使用SimpleInjector进行模拟覆盖.

With this fixed, it's now quite simple to use a subclass of Startup with mocked overrides using SimpleInjector.