10

Each one of my XUnit test projects has an appsettings.json file that specifies a few settings. I have always used dependency injection with IConfiguration to retrieve these settings in my Test Fixture class. Now that I think about it I have absolutely no idea how the IConfiguration was resolved in the past since there is no Startup.cs and ConfigureServices methods in an XUnit project. But I swear it worked.

The following used to work and now it does not:

Fixture:

public class TestFixture : IDisposable
{
    public IConfiguration Configuration { get; set; }

    public TestFixture(IConfiguration configuration)
    {
        Configuration = configuration;
    }
}

Test Class:

public class TestCases : IClassFixture<TestFixture>
{
    public TestCases(TestFixture fixture)
    {

    }
}

The error I am receiving is the following:

Message: System.AggregateException : One or more errors occurred. ---- Class fixture type 'MyProj.Tests.TestFixture' had one or more unresolved constructor arguments: IConfiguration configuration ---- The following constructor parameters did not have matching fixture data: TestFixture fixture

4
  • This would obviously be technically possible but highly unlikely; the xUnit folks have made plenty statements to the effect that there is no plan or desire to do general DI in there both here and on Github issues Commented Apr 18, 2018 at 1:25
  • @RubenBartelink Do you think that is is strange that it somehow worked in the past and does not work now due to the latest version of XUnit or .NET Core 2.0? Commented Apr 18, 2018 at 14:13
  • There's definitely something strange, but I can't think of any reason to believe that there was any point in time when xUnit created things other than that dictated by explicit IClassFixture and/or CollectionAttribute usage. The only thing that's popping into my head is a default constructors hiding the dependencies and/or test classes being private etc. but assume something did work for you so it must be something else Commented Apr 19, 2018 at 11:12
  • Try this xunit di support built into xunit framework: nuget.org/packages/Xunit.Di, so that you can inject services dependencies the same way as you do for any other applications. Commented Oct 3, 2021 at 12:39

2 Answers 2

3



I know this issue is very old but maybe the solution can be useful for someones.

I met with the same issue and fixed it by using WebApplicationFactory generic class. Hopefully, it will be the solution for the others.

replace

: IClassFixture<TestFixture> ->
: IClassFixture<WebApplicationFactory<Startup>>

for more detail, you can visit the official page

Sign up to request clarification or add additional context in comments.

3 Comments

Do you have any code example on this? I made the change you mentioned but I'm still getting the same error.
The following constructor parameters did not have matching fixture data: XXXController
It's already taken from the working code...
0

I wonder why I had code like you since documentation (https://xunit.github.io/docs/shared-context) doesn't say anything about using DI in Fixtures... In my case I solved it by removing the DI, because I had the same error you had.

Then I have such a code to have a services collection for unit tests.

var services = new ServiceCollection();
ConfigureServices(services);
IServiceProvider serviceProvider = services.BuildServiceProvider();

// Assign shortcuts accessors to registered components
UserManager = serviceProvider.GetRequiredService<UserManager<User>>();
RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole<string>>>();

My ConfigureServices() calls this code before registering services to collection, so this answers your question about IConfiguration. Note that I use .net core 2.1 now, had 2.0 before. But I'm still wondering like you and other people who commented, why did the DI not crash before?

private static IConfiguration LoadConfiguration()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");

        return builder.Build();
    }

2 Comments

do you think you can answer this question? Thanks stackoverflow.com/questions/57331395/…
The linked post from @Tom85Morris is very interesting!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.