public void TestCreateBookBad() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <CreateBookDto>(); var service = new CrudServices(context, utData.Wrapped); //ATTEMPT var dto = new CreateBookDto { Title = "", Price = 50, PublishedOn = new DateTime(2010, 1, 1) }; dto.BookAuthorIds.Add(1); dto.BeforeSave(context); service.CreateAndSave(dto); //VERIFY service.IsValid.ShouldBeFalse(); service.GetAllErrors().ShouldEqual("The book title cannot be empty."); } }
public void TestCreateBookOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); var utData = context.SetupSingleDtoAndEntities <CreateBookDto>(); var service = new CrudServices(context, utData.Wrapped); //ATTEMPT var dto = new CreateBookDto { Title = "Hello", Price = 50, PublishedOn = new DateTime(2010, 1, 1) }; dto.BookAuthorIds.Add(1); dto.BeforeSave(context); service.CreateAndSave(dto); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); context.Set <Book>().Count().ShouldEqual(5); var book = context.Books.Include(x => x.AuthorsLink).ThenInclude(x => x.Author) .Single(x => x.BookId == dto.BookId); book.AuthorsLink.Single().Author.Name.ShouldEqual("Martin Fowler"); } }