Unresolved constructor arguments error when trying to use dependency injection with XUnit
Asked Answered
S

2

8

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

Schoolmarm answered 17/4, 2018 at 18:46 Comment(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 issuesRorke
@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?Schoolmarm
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 elseRorke
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.Zellazelle
A
2



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

Argus answered 26/10, 2021 at 14:14 Comment(3)
Do you have any code example on this? I made the change you mentioned but I'm still getting the same error.Glyceric
The following constructor parameters did not have matching fixture data: XXXControllerUniformize
It's already taken from the working code...Argus
A
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();
    }
Angloamerican answered 23/11, 2018 at 21:26 Comment(2)
do you think you can answer this question? Thanks #57331895Hypostyle
The linked post from @Tom85Morris is very interesting!Angloamerican

© 2022 - 2024 — McMap. All rights reserved.