public async Task GetUserFavorites_WithCorrectData_ShouldReturnAllFavoriteProducts() { string onFalseErrorMessage = "The method did not return the correct products."; string onCountDifferenceErrorMessage = "The count of the returned products is not correct"; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); // Gets the user and products var user = this.GetUser(); var products = this.GetMultipleProducts(); // Seed the needed data await this.SeedUser(context); await this.SeedMultipleProducts(context); await this.SeedMultipleFavoriteProducts(context); var userManager = UserManagerInitializer.InitializeMockedUserManager(); userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user); var favoritesService = new FavoritesService(context, userManager.Object); var methodResult = await favoritesService.GetUserFavorites(user.Id); var expectedCountOfProducts = products.Count; var actualCountOfProducts = 0; foreach (var pr in methodResult) { actualCountOfProducts++; var productId = pr.Id; var productName = pr.Name; Assert.True( products.Any(p => p.Id == productId && p.Name == productName), onFalseErrorMessage); } AssertExtensions.EqualCountWithMessage( expectedCountOfProducts, actualCountOfProducts, onCountDifferenceErrorMessage); }
public async Task GetUserFavorites_WithNonExistingUser_ShouldReturnNull() { string onNonNullErrorMessage = "The method did not return a null object."; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); // Sets a fake (non-existing user) ApplicationUser user = null; var userId = "fakeUserId"; var userManager = UserManagerInitializer.InitializeMockedUserManager(); userManager.Setup(x => x.FindByIdAsync(userId)).ReturnsAsync(user); var favoritesService = new FavoritesService(context, userManager.Object); var methodResult = await favoritesService.GetUserFavorites(userId); AssertExtensions.NullWithMessage(methodResult, onNonNullErrorMessage); }
public async Task GetUserFavorites_WithNoFavoriteProducts_ShouldReturnEmptyCollection() { string onNonEmptyCollectionErrorMessage = "The method did not return an empty collection."; var context = ApplicationDbContextInMemoryFactory.InitializeContext(); // Gets the user var user = this.GetUser(); // Seed the needed data await this.SeedUser(context); await this.SeedMultipleProducts(context); var userManager = UserManagerInitializer.InitializeMockedUserManager(); userManager.Setup(x => x.FindByIdAsync(user.Id)).ReturnsAsync(user); var favoritesService = new FavoritesService(context, userManager.Object); var methodResult = await favoritesService.GetUserFavorites(user.Id); AssertExtensions.EmptyWithMessage(methodResult, onNonEmptyCollectionErrorMessage); }