Simple fixture based integration test pattern for MVC
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
src/Xunit.Fixture.Mvc
.gitattributes
.gitignore
README.md
appveyor.yml
xunit-fixture-mvc.sln

README.md

Build status NuGet

xunit-fixture-mvc

MVC functional tests with a fixture pattern.

Example

public class BreakfastItemTests {
    private readonly ITestOutputHelper _output;

    public BreakfastItemTests(ITestOutputHelper output) {
        _output = output;
    }

    [Fact]
    public Task When_creating_breakfast_item() =>
        new MvcFunctionalTestFixture<Startup>(_output)
            .WhenCreating("BreakfastItem", out CreateOrUpdateBreakfastItemRequest request)
            .ShouldReturnSuccessfulStatus()
            .JsonResultShould<BreakfastItem>(r => r.Id.Should().Be(1),
                                                r => r.Name.Should().Be(request.Name),
                                                r => r.Rating.Should().Be(request.Rating))
            .RunAsync();
}

This already reads pretty well but let's break it down:

new MvcFunctionalTestFixture<Startup>(_output)

Configures the test server to use the specified startup class and to send all logs to the xunit test context.

.WhenCreating("BreakfastItem", out CreateOrUpdateBreakfastItemRequest request)

Configures the fixture to send a POST /BreakfastItem request to the test server once it's up and running. We're also asking the fixture to use AutoFixture to create a new instance of the request class and return it to us in an out parameter.

.ShouldReturnSuccessfulStatus()

Adds an assertion to the fixture that the response has a successful (2xx) code.

.JsonResultShould<BreakfastItem>(r => r.Id.Should().Be(1),
                                 r => r.Name.Should().Be(request.Name),
                                 r => r.Rating.Should().Be(request.Rating))

Adds an assertion to the fixture that the response body can be deserialized to an instance of BreakfastItem, that it's id property is 1 and that it's name and rating properties match those of the request.

.RunAsync();

This actually runs the fixture. The configuration will mean that it will:

  1. Create a new test server to host the API that we're testing.
  2. Send a POST /BreakfastItem request to the API with a JSON request body.
  3. Assert that the response has a successful (2xx) code.
  4. Attempt to deserialize the response body and run assertions on the deserialized object.
  5. If no exceptions are thrown, do nothing and let the test pass. Otherwise:
    • If one exception is thrown => re-throw the exception.
    • If multiple exceptions are thrown => throw an aggregate exception containing all thrown exceptions.