Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net core 3.0, issue with IClassFixture, “unresolved constructor arguments: ITestOutputHelper output” #17586

Closed
ranouf opened this issue Dec 4, 2019 · 2 comments
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate

Comments

@ranouf
Copy link

ranouf commented Dec 4, 2019

I made a repo for the issue I have with Integration testing upgrading to .net core 3.0 :
https://github.com/ranouf/TestingWithDotNetCore3_0

When I launch the test, I have this issue:
Message:

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) -----

Here is the constructor:

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

TestStartup:

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

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

TestServerFixture:

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);
        }
    }
}

So clearly, the Iod for TestServerFixture testServerFixture and ITestOutputHelper output doesnt work. How to make it work?

Note: This question is also on StackOverflow

@ranouf
Copy link
Author

ranouf commented Dec 4, 2019

I was thinking that maybe ITestOutputHelper was a service injected by Xunit with "UseTestServer". So I tried in TestServerFixture to do this:

    public TestServerFixture()
    {
        Output = Services.GetRequiredService<ITestOutputHelper>(); ;
        Client = Server.CreateClient();
        InitializeDataBase();
    }

But I got the error:

System.InvalidOperationException: 'No service for type 'Xunit.Abstractions.ITestOutputHelper' has been registered.'

@javiercn javiercn added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate labels Dec 4, 2019
@javiercn javiercn self-assigned this Dec 4, 2019
@javiercn javiercn added this to the 5.0.0-preview1 milestone Dec 4, 2019
@ranouf
Copy link
Author

ranouf commented Dec 7, 2019

Hi,

I found a solution available here: https://stackoverflow.com/a/59222453/6123422

:)

@ranouf ranouf closed this as completed Dec 7, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Jan 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate
Projects
None yet
Development

No branches or pull requests

2 participants