public async Task CreateGenreAsyncThrowsArgumentExceptionWhenGivenAExistingGenreName() { var genres = new List <Genre>(); var mockGenreRepo = new Mock <IGenresRepository>(); mockGenreRepo.Setup(x => x.GenreExist("gosho")).Returns(true); var service = new GenresService(mockGenreRepo.Object); await Assert.ThrowsAsync <ArgumentException>(() => service.CreateGenreAsync(new Web.ViewModels.Genres.CreateGenreInputModel() { Name = "gosho" })); }
public async Task CreateGenreShouldAddToDatabase() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var genresService = new GenresService(db); await genresService.CreateGenreAsync("test", "test description"); Assert.Equal(1, await db.Genres.CountAsync()); }
public async Task CreateGenreShouldReturnCorrectId() { var options = new DbContextOptionsBuilder <AlexandriaDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new AlexandriaDbContext(options); var genresService = new GenresService(db); var id = await genresService.CreateGenreAsync("test", "test description"); var result = await db.Genres.FirstOrDefaultAsync(); Assert.Equal(id, result.Id); }
public async Task CreateGenreAsyncCreatesNewGenreWhenNameIsNotTaken() { var genres = new List <Genre>(); var mockGenreRepo = new Mock <IGenresRepository>(); mockGenreRepo.Setup(x => x.GenreExist("gosho")).Returns(false); mockGenreRepo.Setup(x => x.AddAsync(It.IsAny <Genre>())).Callback((Genre genre) => genres.Add(genre)); var service = new GenresService(mockGenreRepo.Object); await service.CreateGenreAsync(new Web.ViewModels.Genres.CreateGenreInputModel() { Name = "gosho" }); Assert.Single(genres); Assert.Equal("gosho", genres.First().Name); }
public async Task CreateGenreShouldBeCorrect() { AutoMapperConfig.RegisterMappings(typeof(GenreTestModel).Assembly); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Genre>(new ApplicationDbContext(options.Options)); await repository.AddAsync(new Genre { Id = 1, Name = "Drama" }); await repository.AddAsync(new Genre { Id = 2, Name = "Comedy" }); await repository.SaveChangesAsync(); var service = new GenresService(repository, null); await service.CreateGenreAsync("Action"); var genresCount = service.GetAll <GenreTestModel>().Count; Assert.Equal(3, genresCount); }