public async Task RemoveFromFavoritesAsync_WithoutVideoInTheFavoritesList_ShouldThrowAndInvalidOperationException() { //Arrange var expectedErrorMessage = "The given video isn't in the favorites list!"; var option = new DbContextOptionsBuilder <ChessDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var db = new ChessDbContext(option); var moqUserService = new Mock <IUsersService>(); moqUserService.Setup(x => x.GetCurrentUserAsync()) .ReturnsAsync(new ApplicationUser { Id = "UserId", UserName = "******", }); favouritesService = new FavouritesService(db, moqUserService.Object); var testingVideo = new Video { Id = 2, Title = "VideoTitle", }; await db.Videos.AddAsync(testingVideo); await db.SaveChangesAsync(); //Act and assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => favouritesService.RemoveFromFavoritesAsync(2)); Assert.Equal(expectedErrorMessage, ex.Message); }
public async Task RemoveFromFavoritesAsync_WithCurrentUserEqualsToNull_ShouldThrowAndInvalidOperationException() { //Arrange var expectedErrorMessage = "Current user can't be null"; var option = new DbContextOptionsBuilder <ChessDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var db = new ChessDbContext(option); var moqUserService = new Mock <IUsersService>(); favouritesService = new FavouritesService(db, moqUserService.Object); //Act and assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => favouritesService.RemoveFromFavoritesAsync(1)); Assert.Equal(expectedErrorMessage, ex.Message); }
public async Task RemoveFromFavoritesAsync_WithExistingVideoInFavorites_ShouldReturnTrue() { //Arrange var option = new DbContextOptionsBuilder <ChessDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var db = new ChessDbContext(option); var moqUserService = new Mock <IUsersService>(); moqUserService.Setup(x => x.GetCurrentUserAsync()) .ReturnsAsync(new ApplicationUser { Id = "UserId", UserName = "******", UserFavouriteVideos = new List <UserFavouriteVideo> { new UserFavouriteVideo { Id = 1, VideoId = 2, }, } }); favouritesService = new FavouritesService(db, moqUserService.Object); var testingVideo = new Video { Id = 2, Title = "VideoTitle", }; await db.Videos.AddAsync(testingVideo); await db.UserFavouriteVideos.AddAsync(new UserFavouriteVideo { Id = 1, VideoId = 2, ApplicationUserId = "UserId" }); await db.SaveChangesAsync(); //Act var actual = await favouritesService.RemoveFromFavoritesAsync(2); //Assert Assert.True(actual); }
public async Task RemoveFromFavoritesAsync_WithInvalidVideoId_ShouldThrowAnArgumentException() { //Arrange var expectedErrorMessage = "Video with the given id doesn't exist!"; var option = new DbContextOptionsBuilder <ChessDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var db = new ChessDbContext(option); var moqUserService = new Mock <IUsersService>(); moqUserService.Setup(x => x.GetCurrentUserAsync()) .ReturnsAsync(new ApplicationUser { Id = Guid.NewGuid().ToString(), UserName = "******" }); favouritesService = new FavouritesService(db, moqUserService.Object); var ex = await Assert.ThrowsAsync <ArgumentException>(() => favouritesService.RemoveFromFavoritesAsync(1)); Assert.Equal(expectedErrorMessage, ex.Message); }