Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection in Xunit project

I am working on an ASP.Net Core MVC Web application.

My Solution contains 2 projects:

  • One for the application and
  • A second project, dedicated to unit tests (XUnit).

I have added a reference to the application project in the Tests project.

What I want to do now is to write a class in the XUnit Tests project which will communicate with the database through entity framework.

What I was doing in my application project was to access to my DbContext class through constructor dependency injection.

But I cannot do this in my tests project, because I have no Startup.cs file. In this file I can declare which services will be available.

So what can I do to get a reference to an instance of my DbContext in the test class?

like image 324
Bob5421 Avatar asked Jun 19 '18 06:06

Bob5421


People also ask

Does xUnit have setup?

xUnit.net offers several methods for sharing this setup and cleanup code, depending on the scope of things to be shared, as well as the expense associated with the setup and cleanup code.

What is Fact and Theory in xUnit?

Fact vs Theory In an Xunit test class or fixture, there are two kinds of tests: Fact tests and Theory tests. The small, but very important, difference is that Theory tests are parameterized and can take outside input. Fact tests, however, are not parameterized and cannot take outside input.

What is the purpose of xUnit?

It allows you to create new attributes to control your tests. It ensures custom functionality with the possibility of extending the Asset class's Contains, DoesNotContain Equal, NotEqual, InRange, & NotInRange. xUnit also allows you to inherit from Fact, Theory, and other attributes.

Does xUnit run in parallel?

Parallelism in Test FrameworksTests written in xUnit.net version 1 cannot be run in parallel against each other in the same assembly, though multiple test assemblies linked against v1 are still able to participate in the runner parallelism feature described in the next sub-section.


1 Answers

You can implement your own service provider to resolve DbContext.

public class DbFixture {     public DbFixture()     {         var serviceCollection = new ServiceCollection();         serviceCollection             .AddDbContext<SomeContext>(options => options.UseSqlServer("connection string"),                 ServiceLifetime.Transient);           ServiceProvider = serviceCollection.BuildServiceProvider();     }      public ServiceProvider ServiceProvider { get; private set; } }  public class UnitTest1:IClassFixture<DbFixture> {     private ServiceProvider _serviceProvider;      public UnitTest1(DbFixture fixture)     {         _serviceProvider = fixture.ServiceProvider;     }      [Fact]     public void Test1()     {         using (var context = _serviceProvider.GetService<SomeContext>())         {         }     } } 

But bear in your mind using EF inside a unit test is not a good idea and it's better to mock DbContext.

The Anatomy of Good Unit Testing

like image 189
Mohsen Esmailpour Avatar answered Oct 01 '22 22:10

Mohsen Esmailpour