You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.3 KiB

using Model;
using Moq;
namespace UTests3;
public class UnitTests
{
[Fact]
public async Task Test_AddNewNounours_Success()
{
var nounours = new Nounours("chucky.chucky@hell.com");
//setup the mocks
var mockStore = new Mock<INounoursStore>();
mockStore.Setup(store => store.Add(nounours)).ReturnsAsync(() => nounours);
var mockEmailService = new Mock<IEmailService>();
//mockEmailService.Setup(service => service.AskConfirmation(nounours));
//setup the subject-under-test
NounoursManager mgr = new NounoursManager(mockStore.Object, mockEmailService.Object);
//execute the method to test
bool result = await mgr.AddNewNounours(nounours);
//assert the interactions went correctly
Assert.True(result);
mockStore.Verify(store => store.Add(nounours), Times.Once);
mockStore.Verify(store => store.Remove(nounours), Times.Never);
mockEmailService.Verify(store => store.AskConfirmation(nounours), Times.Once);
}
[Fact]
public async Task Test_AddNewNounours_Fail_AlreadyExists()
{
var nounours = new Nounours("chucky.chucky@hell.com");
//setup the mocks
var mockStore = new Mock<INounoursStore>();
mockStore.Setup(store => store.Add(nounours)).ReturnsAsync(() => null);
//mockStore.Setup(store => store.Remove(nounours)).ReturnsAsync(true);
var mockEmailService = new Mock<IEmailService>();
//setup the subject-under-test
NounoursManager mgr = new NounoursManager(mockStore.Object, mockEmailService.Object);
//execute the method to test
bool result = await mgr.AddNewNounours(nounours);
//assert the interactions went correctly
Assert.False(result);
mockStore.Verify(store => store.Add(nounours), Times.Once);
mockStore.VerifyNoOtherCalls();
mockEmailService.VerifyNoOtherCalls();
}
[Fact]
public async Task Test_AddNewNounours_Fail_BadEmail()
{
var nounours = new Nounours("chucky.chucky@hell.com");
//setup the mocks
var mockStore = new Mock<INounoursStore>();
mockStore.Setup(store => store.Add(nounours)).ReturnsAsync(() => nounours);
mockStore.Setup(store => store.Remove(nounours)).ReturnsAsync(true);
var mockEmailService = new Mock<IEmailService>();
mockEmailService.Setup(service => service.AskConfirmation(nounours)).Throws<Exception>();
//setup the subject-under-test
NounoursManager mgr = new NounoursManager(mockStore.Object, mockEmailService.Object);
//execute the method to test
bool result = await mgr.AddNewNounours(nounours);
//assert the interactions went correctly
Assert.False(result);
mockStore.Verify(store => store.Add(nounours), Times.Once);
mockStore.Verify(store => store.Remove(nounours), Times.Once);
mockStore.VerifyNoOtherCalls();
mockEmailService.Verify(service => service.AskConfirmation(nounours), Times.Once);
mockEmailService.VerifyNoOtherCalls();
}
[Fact]
public async Task Test_AddNewNounours_Fail_ExceptionWhenRemoving()
{
var nounours = new Nounours("chucky.chucky@hell.com");
//setup the mocks
var mockStore = new Mock<INounoursStore>();
mockStore.Setup(store => store.Add(nounours)).ReturnsAsync(() => nounours);
mockStore.Setup(store => store.Remove(nounours)).ReturnsAsync(false);
var mockEmailService = new Mock<IEmailService>();
mockEmailService.Setup(service => service.AskConfirmation(nounours)).Throws<Exception>();
//setup the subject-under-test
NounoursManager mgr = new NounoursManager(mockStore.Object, mockEmailService.Object);
//assert it throws an exception
_ = await Assert.ThrowsAsync<Exception>(async () => await mgr.AddNewNounours(nounours));
mockStore.Verify(store => store.Add(nounours), Times.Once);
mockStore.Verify(store => store.Remove(nounours), Times.Once);
mockStore.VerifyNoOtherCalls();
mockEmailService.Verify(service => service.AskConfirmation(nounours), Times.Once);
mockEmailService.VerifyNoOtherCalls();
}
}