c# - .net core 3.0,IClassFixture 问题, "unresolved constructor arguments: ITestOutputHelper output"

标签 c# xunit .net-core-3.0

我为集成测试升级到 .net core 3.0 时遇到的问题做了一个 repo : https://github.com/ranouf/TestingWithDotNetCore3_0

当我启动测试时,我遇到了这个问题: 留言:

System.AggregateException : One or more errors occurred. (Class fixture type 'MyIntegrationTests.TestServerFixture' had one or more unresolved constructor arguments: ITestOutputHelper output) (The following constructor parameters did not have matching fixture data: TestServerFixture testServerFixture) ---- Class fixture type 'MyIntegrationTests.TestServerFixture' had one or more unresolved constructor arguments: ITestOutputHelper output ---- The following constructor parameters did not have matching fixture data: TestServerFixture testServerFixture Stack Trace: ----- Inner Stack Trace #1 (Xunit.Sdk.TestClassException) ----- ----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----

这是构造函数:

public class WeatherForecastController_Tests : IClassFixture<TestServerFixture>
{
    public WeatherForecastController_Tests(TestServerFixture testServerFixture, ITestOutputHelper output)
    {
        Client = testServerFixture.Client;
        Output = output;
    }

测试启动:

public class TestStartup : Startup
{
    public TestStartup(IConfiguration configuration)
        : base(configuration)
    {

    }

    public override void SetUpDataBase(IServiceCollection services)
    {
        // here is where I use the InMemoryDatabase 
    }
}

测试服务器夹具:

public class TestServerFixture : WebApplicationFactory<TestStartup>
{
    private IHost _host;
    public HttpClient Client { get; }
    public ITestOutputHelper Output { get; }

    public TestServerFixture(ITestOutputHelper output)
    {
        Output = output;
        Client = Server.CreateClient();
    }

    // never called but this is where i was previously building up the server
    //
    protected override TestServer CreateServer(IWebHostBuilder builder)
    {
        return base.CreateServer(builder);
    }

    protected override IHost CreateHost(IHostBuilder builder)
    {
        _host = builder.Build();

        using (var scope = _host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;
            InitializeDataBase(services, Output);
        }

        _host.Start();
        return _host;
    }

    protected override IHostBuilder CreateHostBuilder() =>
        Host.CreateDefaultBuilder()
            .ConfigureLogging((hostingContext, builder) =>
            {
                builder.Services.AddSingleton<ILoggerProvider>(new XunitLoggerProvider(Output));
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseTestServer();
            });

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.UseStartup<TestStartup>();
    }

    private void InitializeDataBase(IServiceProvider services, ITestOutputHelper output)
    {
        try
        {
            output.WriteLine("Starting the database initialization.");
            //here is where is feed the Test DB
            output.WriteLine("The database initialization has been done.");
        }
        catch (Exception ex)
        {
            output.WriteLine("An error occurred while initialization the database.");
            Console.WriteLine(ex.Message);
        }
    }
}

很明显,TestServerFixture testServerFixture 和 ITestOutputHelper 输出的 Iod 不起作用。如何让它发挥作用?

最佳答案

感谢@Nkosi 的帮助,它帮助我找到解决方案:)。 我也受到其他网站的启发:

我也更新了我的 repo 上的解决方案

这里是重要的部分:

测试服务器夹具:

public class TestServerFixture : WebApplicationFactory<TestStartup>
{
    public HttpClient Client { get; }
    public ITestOutputHelper Output { get; set; }

    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders(); //All API logging providers are cleared
                logging.AddXunit(Output); //Internal extension which redirect all log to the ITestOutputHelper 
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseStartup<TestStartup>()
                    .ConfigureTestServices((services) =>
                    {
                        //Without that, the client always returns 404
                        //(even if it has already been set in Startup.Configure)
                        services
                            .AddControllers()
                            .AddApplicationPart(typeof(Startup).Assembly);
                    });
            });

        return builder;
    }

    //ITestOutputHelper is set in the constructor of the Test class
    public TestServerFixture SetOutPut(ITestOutputHelper output)
    {
        Output = output;
        return this;
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        Output = null;
    }
}

WeatherForecastController_Tests:

public class WeatherForecastController_Tests : IClassFixture<TestServerFixture>
{
    public TestServerFixture TestServerFixture { get; private set; }
    public HttpClient Client { get; private set; }
    public ITestOutputHelper Output { get { return TestServerFixture.Output; } }

    public WeatherForecastController_Tests(TestServerFixture testServerFixture, ITestOutputHelper output)
    {
        TestServerFixture = testServerFixture.SetOutPut(output);
        Client = testServerFixture.CreateClient();
    }

    [...]
}

如果您有任何改进代码的建议或问题,请告诉我:)

关于c# - .net core 3.0,IClassFixture 问题, "unresolved constructor arguments: ITestOutputHelper output",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59166798/

相关文章:

c# - 从谷歌发送电子邮件在生产服务器上失败但在本地主机上工作正常

c# - 单元测试中的 ActionResult 问题无法断言。POST 返回值等于(asp.net core mvc web api)

c# - 为了最小化测试范围内的单元,我是否应该在单元测试时始终使用 Mocks

entity-framework-core - 我无法运行迁移(使用 .net core 3.0 和 Entity Framework )

reactjs - 识别 SPA 和 .NET Core 3 的角色

c# - JsonConverter 等效于使用 System.Text.Json

c# - 当 Visual Studio 只允许它是 "Application"设置时,我如何允许用户更改 SQL 连接字符串?

c# - C++ 中的 C# SecureCode 是什么?

c# - Python 中 Rawinput 的 C# 等价物是什么?

c# - 为泛型方法创建单元测试