public async Task WhenDelete_AndRecordFound_ThenRepoCalled() { // Arrange var entity = new Infrastructure.Entities.Book { Id = Guid.NewGuid().ToString(), Author = "Author", Name = "Name", Year = "2018" }; var mockId = string.Empty; var repo = new Mock <IRepository>(); repo.Setup(x => x.GetBook(It.IsAny <string>())) .Callback((string id) => mockId = id) .Returns(Task.FromResult(entity)); repo.Setup(x => x.DeleteBook(It.IsAny <string>())).Returns(Task.CompletedTask); var service = new BookService(repo.Object); // Act await service.DeleteBook(entity.Id); // Assert Assert.AreEqual(entity.Id, mockId); repo.Verify(x => x.GetBook(It.IsAny <string>()), Times.Once, "Service was not called or called more than once"); repo.Verify(x => x.DeleteBook(It.IsAny <string>()), Times.Once, "Service was not called or called more than once"); }
public async Task WhenUpdate_AndRequestOk_ThenRepoCalled() { // Arrange var book = CreateBook(); var entity = new Infrastructure.Entities.Book() { Id = book.Id, Year = book.Year, Author = book.Author, Name = book.Name }; var entityArg = new Infrastructure.Entities.Book(); var repo = new Mock <IRepository>(); repo.Setup(x => x.GetBook(It.IsAny <string>())).Returns(Task.FromResult(entity)); repo.Setup(x => x.UpdateBook(It.IsAny <Infrastructure.Entities.Book>())) .Callback((Infrastructure.Entities.Book updateEntity) => { entityArg.Id = updateEntity.Id; entityArg.Name = updateEntity.Name; entityArg.Year = updateEntity.Year; entityArg.Author = updateEntity.Author; }) .Returns(Task.CompletedTask); var service = new BookService(repo.Object); // Act await service.UpdateBook(book); // Assert repo.Verify(x => x.GetBook(It.IsAny <string>()), Times.Once, "Repo was not called or called more than once"); repo.Verify(x => x.UpdateBook(It.IsAny <Infrastructure.Entities.Book>()), Times.Once, "Repo was not called or called more than once"); Assert.AreEqual(book.Id, entityArg.Id); Assert.AreEqual(book.Name, entityArg.Name); Assert.AreEqual(book.Year, entityArg.Year); Assert.AreEqual(book.Author, entityArg.Author); }