使用WebApplicationFactory时如何在启动中修复SimpleInjector InvalidOperationException?

时间:2019-09-12 14:14:03

标签: c# integration-testing xunit

嗨,我在解决方案中运行所有集成测试时遇到问题。

这是我的代码:

CustomWebApplicationFactory

dates %>% 
   map(.,format,"%m")
$YrMon
[1] "01" "02" "03" "04"

基类

with(dates, format(YrMon, "%m"))
[1] "01" "02" "03" "04"

例外:

public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
    {
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.UseEnvironment("Development");
            base.ConfigureWebHost(builder);
        }
    }

我必须在两个类中进行简单的测试,但实际上我什么都不做。像这样通过IntegrationTest继承:

  public class IntegrationTest : IClassFixture<CustomWebApplicationFactory<Startup>>, IDisposable
    {
        public readonly CustomWebApplicationFactory<Startup> Factory;
        public readonly HttpClient HttpClient;
        public readonly WebSocketClient WebSocketClient;
        public IntegrationTest(CustomWebApplicationFactory<Startup> factory)
        {
            this.Factory = factory;
            HttpClient = factory.CreateDefaultClient();
            WebSocketClient = factory.Server.CreateWebSocketClient();
        }

        public void Dispose()
        {
            Factory?.Server?.Dispose();
            Factory?.Dispose();
            HttpClient?.Dispose();
        }
    }

我已对此进行调试,并且在第二次执行中,我在此行中遇到此错误:


System.InvalidOperationException : The AddSimpleInjector extension method can only be called once.
   at SimpleInjector.SimpleInjectorServiceCollectionExtensions.AddSimpleInjectorOptions(Container container, SimpleInjectorAddOptions builder)
   at SimpleInjector.SimpleInjectorServiceCollectionExtensions.AddSimpleInjector(IServiceCollection services, Container container, Action`1 setupAction)
   at psitrans.DataConverting.WebService.Startup.<>c__DisplayClass6_0.<ConfigureServices>g__ConfigureSimpleInjector|2() in C:\Repos\PSITraffic\PSItraffic.NET\Dev\Web\DataConvertingWebService\DataConvertingWebService\DataConvertingWebService\Startup.cs:line 87
   at psitrans.DataConverting.WebService.Startup.ConfigureServices(IServiceCollection services) in C:\Repos\PSITraffic\PSItraffic.NET\Dev\Web\DataConvertingWebService\DataConvertingWebService\DataConvertingWebService\Startup.cs:line 100
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateServer(IWebHostBuilder builder)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
   at psitrans.DataConverting.WebServiceIntegrationTests.IntegrationTest..ctor(CustomWebApplicationFactory`1 factory) in C:\Repos\PSITraffic\PSItraffic.NET\Dev\Web\DataConvertingWebService\DataConvertingWebService\DataConvertingWebServiceIntegrationTests\IntegrationTest.cs:line 18
   at psitrans.DataConverting.WebServiceIntegrationTests.ConvertOData.ConvertODataVehicleFaultTest..ctor(CustomWebApplicationFactory`1 factory) in C:\Repos\PSITraffic\PSItraffic.NET\Dev\Web\DataConvertingWebService\DataConvertingWebService\DataConvertingWebServiceIntegrationTests\ConvertOData\ConvertODataVehicleFaultTest.cs:line 21


我尝试了

  public class Test1: IntegrationTest
    {
        public Test1(CustomWebApplicationFactory<Startup> factory) : base(factory) { }
    }

但这无济于事。

有什么解决办法吗?

我什至可以单独运行所有测试,但是最好由Visual Studio中的xunit运行器来完成。

0 个答案:

没有答案