Integration testing with Entity Framework Core 2.0

Few months ago I wrote a post about how to use the new InMemoryDbContext of Entity Framework Core in our unit tests and substitute the real context with an in-memory implementation, avoiding to connect to the real database.

Now that Entity Framework Core 2.0 has been released, I upgraded my project and I found a new nicely interface that we can use in our integration tests to provide the configurations and the connection string to the context.

This interface is IDesignTimeDbContextFactory.

IDesignTimeDbContextFactory

This is a context typed interface, the base implementation of this interface is pretty simple and is looks like this:

public class BloggingContextFactory : IDesignTimeDbContextFactory<CoreContext>
{
public CoreContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<CoreContext>();
optionsBuilder.UseSqlServer("Data Source=.\\sqlexpress;Initial Catalog=CoreContextEFCore;User Id=;Password=;MultipleActiveResultSets=True;");

return new CoreContext(optionsBuilder.Options);
}
}

This class working like a real factory, so we create a new DbContextOptionsBuilder, we define the connection string in the UseSqlServer method and we return a new instance of the context.

As you can see from the name, this is an interface with a “Design Time” purpose, so we use it in situations where we don’t have a real configuration like in an ASP.NET application but we need to provide at design time a specific configuration to the context.

Infact, I implemented this class in my unit test class library project; Entity framework will use this class if we refer to the test project as the startup project in the migrations command:

dotnet ef migrations add InitialMigration -s ..\EFCore.Test\
dotnet ef database update -s ..\EFCore.Test\

And I can use this factory in the test methods as well.

TESTS

I use NUnit as a test framework, now fully compatible with .NET Core.

In order to run our tests we need to install three nuget packages in the class library:

The first one is the Microsoft suite to build .NET core projects and run dotnet test console command.

The second one is NUnit and the third one is NUnit adapter to run the test from Visual Studio UI; differently from the classic .NET projects, we no longer need to install the VSIX adapter package.

Now we can implement our test methods like this:

[TestFixture]
public class ContextTest
{
private BloggingContextFactory _bloggingContextFactory;

[OneTimeSetUp]
public void Setup()
{
_bloggingContextFactory = new BloggingContextFactory();
}

[Test]
public async Task CreateBlogs()
{
using (var db = _bloggingContextFactory.CreateDbContext(new string[]{}))
{
for (int i = 0; i < 1000; i++)
{
var blog = new Blog()
{
Name = "Test"
};
db.Blogs.Add(blog);
}

await db.SaveChangesAsync();
}
}
}

We leverage the context factory implemented in the tests methods to generate instances of the context with the parameters defined in the factory.

After that, we can run the tests from the Visual Studio UI by using the NUnit Test Adapter or by the console by using Microsoft Test SDK with the command dotnet test.

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: