Dependency Injection with XUnit and ASP.NET Core 1.0

Viewed 6386

I am trying to figure out how I can use dependency injection with XUnit. My goal is to be able to inject my ProductRepository into my test class.

Here is the code I am trying:

public class DatabaseFixture : IDisposable
{
    private readonly TestServer _server;

    public DatabaseFixture()
    {
        _server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
    }

    public void Dispose()
    {
        // ... clean up test data from the database ...
    }
}

public class MyTests : IClassFixture<DatabaseFixture>
{
    DatabaseFixture _fixture;
    public ICustomerRepository _repository { get; set; }

    public MyTests(DatabaseFixture fixture, ICustomerRepository repository)
    {
        _fixture = fixture;
        _repository = repository;
    }
}

Here is the error: The following constructor parameters did not have matching fixture data (ICustomerRepository repository)

This leads me to believe that XUnit doens't support dependency injection, only if it is a Fixture.

Can someone give me a way of getting an instance of ProductRepository in my test class using XUnit? I believe I am correctly starting up a test server so Startup.cs runs and configures the DI.

1 Answers
Related