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

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • 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 – Ruben Bartelink Apr 18 '18 at 01: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? – Blake Rivell Apr 18 '18 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 – Ruben Bartelink Apr 19 '18 at 11:12
  • Try this xunit di support built into xunit framework: https://www.nuget.org/packages/Xunit.Di/, so that you can inject services dependencies the same way as you do for any other applications. – cli Oct 03 '21 at 12:39

2 Answers2

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

malik masis
  • 487
  • 1
  • 6
  • 15
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();
    }
barbara.post
  • 1,581
  • 16
  • 27
  • 1
    do you think you can answer this question? Thanks https://stackoverflow.com/questions/57331395/net-core-execute-all-dependency-injection-in-xunit-test-for-appservice-reposit?noredirect=1&lq=1 –  Aug 04 '19 at 18:41
  • The linked post from @Tom85Morris is very interesting! – barbara.post Aug 06 '19 at 08:32