Now I have to do some code coverage . The easy way is to test the web application – doing an integration test. Use this as a starting point: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-5.0
I have 2 kind of tests: HappyPath -when all is working ok – and TestErrors – when it is not.
The code for all working ok is
public async void TestFakeUser()
{
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetStringAsync("/recordVisitors/AllVisitors5Min");
// Assert
var str = "Jean Irvine";
Assert.True(response.Contains(str),$"{response} must contain {str}");
}
and for testing errors
[Fact]
public void TestFakeUser()
{
_factory.RemoveServices = true;
_factory.RemoveFakeUser = false;
var ex = Record.Exception(()=>_factory.CreateClient());
Assert.IsType<ArgumentException>(ex);
}
[Fact]
public async void TestNoUser()
{
_factory.RemoveServices = false;
_factory.RemoveFakeUser= true;
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetStringAsync("/recordVisitors/AllVisitors5Min");
// Assert
var str = "Jean Irvine";
Assert.True(response.Contains(str),$"{response} must contain {str}");
}
The code coverage has reached 94% after 3 iterations – it is ok. You can see the results at https://app.codecov.io/gh/ignatandrei/RecordVisitors
Please make sure that you read also https://andrewlock.net/converting-integration-tests-to-net-core-3/
Leave a Reply