/NFixtures

Primary LanguageC#MIT LicenseMIT

NFixtures
NFixtures

A set of fixtures to use in integration tests.

GitHub license

NFixtures give a set of pre-built fixtures you can inject into your tests.

License

Getting started

PackageName Description
NFixtures.WebApi Contains StartupFixture that allows you to easily test your WebApi by passing your Startup
NFixtures.xUnit Gives you a LabeledTestCase that allows you to give a name for your test cases
NFixtures.Primitives Contains a TestUser

StartupFixture

public class ApiFixture : StartupFixture<Startup>
{
    protected override void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder)
    {
        // configure web host configuration - add env parameters, etc.
    }

    protected override void ConfigureTestServices(IServiceCollection services)
    {
        // add additional services (usually Mocks) to use in your test
    }
}

Now you can inject this into your tests, by implementing IClassFixture<ApiFixture>

public class ControllerTests : IClassFixture<ApiFixture>
{
    private readonly ApiFixture _fixture;

    public ControllerTests([NotNull] ApiFixture fixture, [NotNull] ITestOutputHelper output)
    {
        _fixture = fixture;
        _fixture.SetLogger(output); // redirects logger messages into standard xunit test output
    }

    [Fact]
    public async Task Get_Returns_Unauthorized()
    {
        // arrange
        var client = _fixture.CreateDefaultClient();

        // act
        var response = await client.GetAsync("/api/v1/controller").ConfigureAwait(false);

        // assert
        Assert.NotNull(response);
        Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
    }
}

TestUser

public class ApiFixture : StartupFixture<TestStartup>
{
    public ApiFixture()
    {
        FirstUser = new TestUser("123", "email@server.com");
    }
    
    public TestUser FirstUser { get; }
        
    protected override void ConfigureTestServices(IServiceCollection services)
    {
        services
            .ConfigureTestAuthentication(FirstUser); // enable authentication for our TestUser
    }
}