Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 791
Blake Rivell Avatar asked Apr 17 '18 18:04

Blake Rivell


People also ask

How to use dependency injection in an xUnit Test Project?

Several key steps are involved in utilizing dependency injection in an xUnit test project covered below. Defining a class to retain dependency injection provider’s instance We need to define a class to instantiate Microsoft’s dependency injection service provider to add services and configurations.

Should xUnit use the default parameterless constructor?

Ideally for me xUnit should use the default parameterless constructor if it's available instead of throwing an exception public class ConfigurationFixture { public string ConnectionString { get; } public ConfigurationFixture () { var timestamp = DateTimeOffset.

Should you run integration tests by dependency injection?

There are certainly cases when developers want to run integration tests against their code wired up heavily by dependency injection. xUnit is a mature, loved-by-developers, and robust test framework for testing .net code. A slight downside of xUnit is that it lacks an out-of-the-box solution to inject dependencies.

What are the downsides of using xUnit?

A slight downside of xUnit is that it lacks an out-of-the-box solution to inject dependencies. Fortunately, xUnit has been equipped with the “Fixture” feature, which is meant to share contexts among test classes or tests within the same test class.


1 Answers



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

like image 156
malik masis Avatar answered Oct 15 '22 20:10

malik masis