two clear glass jars beside several flasks
.NET

ASP.Net Core WebAPI integration testing with Entity Framework Core

After recently having written about a possible approach to integration test an OWIN/EFCore setup, I thought I would share a minimal setup for doing the same with ASP.Net Core WebAPI and Entity Framework Core. There are a lot of great examples out there, and the documentation is really good – however, I feel I’m always lacking the example which is the absolute minimum of what you need to get things running. No excess NuGet packages and no excess lines in Startup.cs.

You will find the complete source code on GitHub.

NuGet packages needed in the test project

You need a couple of NuGet packages to get this to work:

Also needed is of course the packages for your favorite test framework and Entity Framework Core itself.

Startup.cs

using Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Api
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvcCore().AddJsonFormatters();
            ConfigureDatabase(services);
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc();
        }
        protected virtual void ConfigureDatabase(IServiceCollection services)
        {
            services.AddDbContext<MyContext>(options => options.UseSqlServer("MyConnectionString"));
        }
    }
}

I’m using the virtual method approach to be able to override it in the TestStartup class.

TestStartup.cs

using Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Api.IntegrationTest
{
    public class TestStartup : Startup
    {
        protected override void ConfigureDatabase(IServiceCollection services)
        {
            services.AddDbContext<MyContext>(o => o.UseInMemoryDatabase("MyDatabaseName"));
        }
    }
}

Overriding the virtual method from the Startup class lets you configure the EF context to use an in-memory database.

ValuesControllerTest.cs

using System.Net.Http;
using System.Threading.Tasks;
using Data;
using Data.Model;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
namespace Api.IntegrationTest
{
    [TestClass]
    public class ValuesControllerTest
    {
        public HttpClient Client { get; set; }
        public TestServer Server { get; set; }
        public MyContext Context { get; set; }

        [TestInitialize]
        public void Init()
        {
            var webHostBuilder = WebHost.CreateDefaultBuilder().UseStartup<TestStartup>();
            Server = new TestServer(webHostBuilder);
            Client = Server.CreateClient();
            Context = Server.Host.Services.GetRequiredService<MyContext>();
        }

        [TestMethod]
        public async Task TestMethod1()
        {
            //arrange
            await Context.AddAsync(new Value {Id = 1, Epicness = 1337});
            await Context.SaveChangesAsync();

            //act
            var result = await Client.GetAsync("/values/1");
            result.EnsureSuccessStatusCode();
            var deserializedResult = JsonConvert.DeserializeObject<Value>(await result.Content.ReadAsStringAsync());

            //assert
            Assert.AreEqual(1337, deserializedResult.Epicness);
        }
    }
}

Download the example project and create your own flavor!

two clear glass jars beside several flasks

Leave a Reply

Your email address will not be published. Required fields are marked *