public async Task Create_New_Book()
        {
            using (var context = GetContextWithData())
            {
                var handler    = new CreateBookCommandHandler(context);
                var categoryId = Guid.NewGuid();

                await AddCategory(context, categoryId, "nothing");

                var command = new CreateBookCommand
                {
                    Title      = "Title",
                    Categories = new List <CreateBookModelCategory> {
                        new CreateBookModelCategory {
                            Id = categoryId
                        }
                    }
                };

                await handler.Handle(command, CancellationToken.None);

                var book = await context.Books.SingleOrDefaultAsync(c => c.Title == command.Title);

                Assert.Equal(command.Title, book.Title);
                Assert.Equal(command.Categories.ToList().Count, book.BookCategories.ToList().Count);
                Assert.Equal(command.Categories.FirstOrDefault()?.Id, book.BookCategories.FirstOrDefault()?.CategoryId);
            }
        }
Example #2
0
        public void Should_Create_When_Command_Is_Valid()
        {
            //Arrange
            var createBookCommand = new CreateBookCommand
            {
                Title       = "Clean Code",
                Publication = _publication,
                AuthorId    = Guid.NewGuid(),
                PublisherId = Guid.NewGuid()
            };

            //Act
            _createBookCommandHandler.Handle(createBookCommand);

            //Assert
            _bookRepository.Verify(r => r.Add(It.IsAny <Domain.Entities.Book>()), Times.Once);
            _eventPublisher.Verify(p => p.Publish(It.IsAny <BookEvent>()), Times.Once);
        }