public void PostWithValidObject() { MockGenreService.Setup(r => r.Create(It.IsAny <GenreBO>())).Returns((GenreBO newGenre) => newGenre); var result = _controller.Post(MockGenre); Assert.IsType <CreatedResult>(result); }
public async Task CreateGenre() { //Preparation var databaseName = Guid.NewGuid().ToString(); var context = BuildContext(databaseName); var mapper = BuildMap(); var newGenre = new GenreForCreationDto() { Name = "New Genre" }; //Testing var controller = new GenresController(context, mapper); var response = await controller.Post(newGenre); //Verification var result = response as CreatedAtRouteResult; Assert.AreEqual(201, result.StatusCode); var context2 = BuildContext(databaseName); var count = await context2.Genres.CountAsync(); Assert.AreEqual(1, count); }
public async Task CreateGenre_ShouldReturn201AndAddEntryInDb() { // Arrange var databaseName = Guid.NewGuid().ToString(); BuildGenreTestDatabase(databaseName); var createDbContext = BuildContext(databaseName); var verifyDbContext = BuildContext(databaseName); var mapper = BuildMapper(); var expectedName = "Test Genre"; var expectedCount = createDbContext.Genres.Count() + 1; var createGenreDTO = new CreateGenreDTO() { Name = expectedName }; var controller = new GenresController(createDbContext, mapper); // Act var response = await controller.Post(createGenreDTO); var result = response as CreatedAtRouteResult; var actualName = verifyDbContext.Genres.Find(expectedCount).Name; var actualCount = verifyDbContext.Genres.Count(); // Assert Assert.AreEqual(StatusCodes.Status201Created, result.StatusCode); Assert.AreEqual(expectedName, actualName); Assert.AreEqual(expectedCount, actualCount); }
public void Initialize() { // Cria primeiro o gĂȘnero de Teste var genresController = new GenresController(); var genre = new Genre { ID = 1, Nome = "GĂȘnero de Teste", Ativo = true }; genresController.Post(genre); }
private static void FillDatabaseWithAdditionalChecks(BookLibraryContext context) { // Entity Controllers var authorsController = new AuthorsController(new AuthorsRepository(context), null); var editionsController = new EditionsController(new EditionsRepository(context), null); var genresController = new GenresController(new GenresRepository(context), null); var volumesController = new VolumesController(new VolumesRepository(context), null); var volumeExemplarsController = new VolumeExemplarsController(new VolumeExemplarsRepository(context), null); var worksController = new WorksController(new WorksRepository(context), null); var workKindsController = new WorkKindsController(new WorkKindsRepository(context), null); // Entity Link Controllers var editionVolumeLinksController = new EditionVolumeLinksController(new EditionVolumeLinksRepository(context), null); var volumeWorkLinksController = new VolumeWorkLinksController(new VolumeWorkLinksRepository(context), null); var workAuthorLinksController = new WorkAuthorLinksController(new WorkAuthorLinksRepository(context), null); // Filling and checking IActionResult result; Author authorJackLondon; Author authorTheodoreDreiser; Genre genreBellesletres; Genre genreRealism; Work workMartenEden; Work workWhiteFang; Work workFinancier; Work workSisterCarrie; result = authorsController.Get(0); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); result = authorsController.Put(new Author { Id = 0, Name = "Jack London" }); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); result = authorsController.Post(null); Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); Assert.IsTrue(((BadRequestObjectResult)result).Value.Equals("Entity is null.")); result = authorsController.Post(new Author { Name = "Jack London" }); Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); Assert.IsInstanceOfType(((CreatedAtActionResult)result).Value, typeof(Author)); authorJackLondon = (Author)((CreatedAtActionResult)result).Value; Assert.IsTrue(authorJackLondon.Id != 0); result = authorsController.Post(new Author { Name = "Theodore Dreiser" }); Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); Assert.IsInstanceOfType(((CreatedAtActionResult)result).Value, typeof(Author)); authorTheodoreDreiser = (Author)((CreatedAtActionResult)result).Value; Assert.IsTrue(authorTheodoreDreiser.Id != authorJackLondon.Id); result = authorsController.GetCount(); Assert.IsInstanceOfType(result, typeof(OkObjectResult)); Assert.IsInstanceOfType(((OkObjectResult)result).Value, typeof(int)); Assert.IsTrue((int)((OkObjectResult)result).Value == 2); authorTheodoreDreiser.Name = "Theodore Herman Albert Dreiser"; result = authorsController.Put(authorTheodoreDreiser); Assert.IsInstanceOfType(result, typeof(OkObjectResult)); Assert.IsInstanceOfType(((OkObjectResult)result).Value, typeof(Author)); authorTheodoreDreiser = (Author)((OkObjectResult)result).Value; Assert.IsTrue(authorTheodoreDreiser.Name == "Theodore Herman Albert Dreiser"); genreBellesletres = (Genre)((CreatedAtActionResult)genresController.Post(new Genre { Name = "Belles-letres" })).Value; genreRealism = (Genre)((CreatedAtActionResult)genresController.Post(new Genre { Name = "Realism" })).Value; workMartenEden = (Work)((CreatedAtActionResult)worksController.Post(new Work { Name = "Marten Eden", GenreId = genreBellesletres.Id })).Value; workWhiteFang = (Work)((CreatedAtActionResult)worksController.Post(new Work { Name = "White Fang", GenreId = genreBellesletres.Id })).Value; workFinancier = (Work)((CreatedAtActionResult)worksController.Post(new Work { Name = "Financier", GenreId = genreRealism.Id, AltGenreId = genreBellesletres.Id })).Value; workSisterCarrie = (Work)((CreatedAtActionResult)worksController.Post(new Work { Name = "Sister Carrie", GenreId = genreRealism.Id, AltGenreId = genreBellesletres.Id })).Value; result = workAuthorLinksController.Post(new WorkAuthorLink { WorkId = workMartenEden.Id, AuthorId = authorJackLondon.Id }); Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); result = workAuthorLinksController.Post(new WorkAuthorLink { WorkId = workWhiteFang.Id, AuthorId = authorJackLondon.Id }); Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); result = workAuthorLinksController.Post(new WorkAuthorLink { WorkId = workFinancier.Id, AuthorId = authorTheodoreDreiser.Id }); Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); result = workAuthorLinksController.Post(new WorkAuthorLink { WorkId = workSisterCarrie.Id, AuthorId = authorTheodoreDreiser.Id }); Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); }