public async Task AddBookToAuthorShouldBeCalled() { //arrange var authorId = Fixture.Create <int>(); var bookForCreation = Fixture.Create <BookForCreationDTO>(); var mockAuthorsRepository = new Mock <IAuthorRepository>(); var bookEntity = Mapper.Map <Book>(bookForCreation); var mockBooksRepository = new Mock <IBooksRepository>(); mockBooksRepository .Setup(repo => repo.AddBookToAuthor(authorId, It.IsAny <Book>())); var mockCreateLinksStrategy = new Mock <ICreateBookLinksStrategy>(); mockCreateLinksStrategy .Setup(x => x.CreateLinksForBookResource(It.IsAny <BookDTO>())) .Returns <BookDTO>(x => x); var sut = new CreateBookStrategy( Mapper, mockBooksRepository.Object, mockAuthorsRepository.Object, mockCreateLinksStrategy.Object); //act var result = await sut.CreateBook(authorId, bookForCreation); //assert mockBooksRepository.Verify(x => x.AddBookToAuthor(authorId, It.IsAny <Book>())); mockCreateLinksStrategy.Verify(x => x.CreateLinksForBookResource(It.IsAny <BookDTO>())); }
public ICommandResult Handle(CreateBookCommand command) { var book = new Book( command.Author, command.Title, new Category(command.Category), command.Publishing, command.Edition, command.ISBN, command.CodeBar, command.Year, command.PageNumber, command.Synopsis, command.Height, command.Width, command.Length, command.Weight, new PriceGroup(command.PricingGroup), command.Active ); var strategy = new CreateBookStrategy(); var strategyResult = (GenericCommandResult)strategy.Execute(book, _repository); if (!strategyResult.Success) { return(strategyResult); } _repository.CreateBook(book); _repository.SaveChanges(); return(new GenericCommandResult(true, "Livro criado com sucesso", book)); }
public async Task ShouldCreateNewBook() { //arrange var authorId = Fixture.Create <int>(); var bookForCreation = Fixture.Create <BookForCreationDTO>(); var mockAuthorsRepository = new Mock <IAuthorRepository>(); var mockBooksRepository = new Mock <IBooksRepository>(); mockBooksRepository .Setup(repo => repo.AddBookToAuthor(authorId, It.IsAny <Book>())) .Callback <int, Book>((authId, newBook) => { newBook.AuthorId = authorId; newBook.Author = Fixture.Create <Author>(); newBook.BookId = Fixture.Create <int>(); }); var mockCreateLinksStrategy = new Mock <ICreateBookLinksStrategy>(); mockCreateLinksStrategy .Setup(x => x.CreateLinksForBookResource(It.IsAny <BookDTO>())) .Returns <BookDTO>(x => x); var sut = new CreateBookStrategy( Mapper, mockBooksRepository.Object, mockAuthorsRepository.Object, mockCreateLinksStrategy.Object); //act var result = await sut.CreateBook(authorId, bookForCreation); //assert Assert.AreNotEqual(0, result.Id); Assert.AreEqual(result.Title, bookForCreation.Title); }