In .NET Core for unit testing, I'm using Xunit, Moq, and Autofixture. But even with them, I see that my unit tests become complicated and take time.
Maybe someone could tell me if there are any ways to make this test smaller?
[Fact]
public async Task Verify_NotAuthorised_NoServiceSendInvoked()
{
// Arrange
var fixture = new Fixture()
.Customize(new AutoMoqCustomization());
var sut = fixture.Create<VerificationService>();
var mockApiBuilder = fixture.Freeze<Mock<IApiEntityBuilder>>();
//init mocked mockSendServiceOne, so later I could check if it was invoked or not
var mockSendServiceOne = fixture.Freeze<Mock<ISendServiceOne>>();
mockApiBuilder.Setup(x => x.Verification(It.IsAny<string>(), It.IsAny<string>()))
.Returns(fixture.Create<VerificationEntity>());
var call = fixture.Freeze<Mock<ISendServiceTwo>>();
call.Setup(x => x.IsSuccessful()).Returns(false);
// Act
await sut.Verify(fixture.Create<string>(), fixture.Create<string>());
// Assert
mockSendServiceOne.Verify(x => x.Call(It.IsAny<SendServiceOneEntity>()), Times.Never);
}
The testing method itself
public async Task<CreatedEntity> Verify(string dataOne, string dataTwo)
{
await _someCaller.Call(_apiEntityBuilder.Verification(dataOne, dataTwo));
_someCaller.CreatePayment();
if (!_someCaller.IsSuccessful()) return _someCaller.CreatedEntity;
await mockSendServiceOne.Call(_apiEntityBuilder.Call(_someCaller.CreatedEntity.SpecificData));
return _someCaller.CreatedEntity;
}
Here I am testing if isSuccessful() returns fasle then no mockSendServiceOne.Call should be invoked.
Could someone give me some feedback on how to write a better unit tests. Because only for this small check of code I had to write a lot of code to test it.