Add test data in integration tests

asp.net-core c# entity-framework-core integration-testing xunit

Question

I'm writing integration tests for my .NET Core app using xUnit. I have various things I want to test, but for brevity I'll use this example. I need to test these things ("document" is just a string, I'm not dealing with files):

  • Register new employee
  • Log in as employee
  • Add a new document for the employee
  • Add a comment to the document

Tests are of course supposed to run independently of each other, but I cannot add a comment to a nonexistent document. I need the document to exist to test adding a comment, but the employee must also exist to add the document.

I have test classes written for all entities I want to use, but I don't know how to actually get them in the database for a given test. How can I "seed" the DB for a given test?

TestStartup.cs:

public class TestFixture : IDisposable
{
    readonly TestServer _server;
    public HttpClient _client { get; }

    public TestFixture()
    {
        var builder = new WebHostBuilder()
            .UseContentRoot(@"..\..\..\..\..\src\MyProj.Web")
            .UseEnvironment("IntegrationTestDevelopment")
            .UseStartup<Web.Startup>();

        _server = new TestServer(builder);

        _client = _server.CreateClient();
        _client.BaseAddress = new Uri("http://localhost:2856");
    }

    public void Dispose()
    {
        _client.Dispose();
        _server.Dispose();
    }
}

Relevant part of Web.Startup:

services.AddEntityFrameworkInMemoryDatabase()
    .AddDbContext<MyContext>(options => options.UseInMemoryDatabase());

Example test class:

public class EmployeeTests : IClassFixture<TestFixture>
{
    public HttpClient _client;

    public EmployeeTests(TextFixture fixture)
    {
        _client = fixture._client;
    }

    [Theory]
    [MemberData("ValidRegistrations")]
    public async Task Register_new_employee_succeeds(EmployeeModel model)
    {
        var request = Helpers.CreateRequestMessage("POST", "/api/employees", model);
        var response = await _client.SendAsync(request);
        Assert.Equal(HttpStatusCode.Created, response.StatusCode);
    }
}
1
0
10/15/2016 6:15:36 PM

Popular Answer

I may be missing something here (knowledge of, but never used, .Net Core), but as you say this is an integration test rather than a pure unit test, so would an alternative approach be:

  • Have the integration test merely test the call / connectivity to the service (i.e to test that 'employees' can be called I think in your scenario - regardless of database content)
  • Create a new test (ideally a unit test, but let's face it integration tests definately have a place in this world) to test the functionality of the 'employees' method / function

If the above isn't an option for you, how about considering seeding the db in the test class (one of the method or class set up methods - I forget their names in xUnit) by calling simple database interaction code, doesn't have to be anything exotic or fancy if it's just a seed followed by a subsequent end-of-test tear down. A basic db connection and query execution would presumably suffice.

Appreciate this doesn't directly answer your question, more of an outsiders viewpoint.

0
10/15/2016 8:29:47 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow