NFixtures give a set of pre-built fixtures you can inject into your tests.
- NFixtures is licensed under the MIT License
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 |
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);
}
}
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
}
}