public async Task AddAsyncShouldThrowArgumentExceptionForExistingEntity()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddAsyncShouldThrowArgumentExceptionForExistingEntity").Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <Favourite>(dbContext);
            var service    = new FavouritesService(repository);

            await service.AddAsync(1, "test");

            await Assert.ThrowsAsync <ArgumentException>(() => service.AddAsync(1, "test"));
        }
        public async Task AddAsyncShouldReturnFalseWhenProductIdDoesNotExistUsingMoq()
        {
            var favouriteProductsRepository = new Mock <IRepository <UserFavouriteProduct> >();
            var productsRepository          = new Mock <IDeletableEntityRepository <Product> >();

            var productImages = new List <ProductImage>
            {
                new ProductImage {
                    ImageUrl = "TestImageUrl1"
                },
                new ProductImage {
                    ImageUrl = "TestImageUrl2"
                },
            };

            var productsList = new List <Product>
            {
                new Product {
                    Id = "TestProductId", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>()
                },
            };

            productsRepository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable());

            var service = new FavouritesService(favouriteProductsRepository.Object, productsRepository.Object, null);

            Assert.False(await service.AddAsync("InvalidId", "TestUserId"));

            productsRepository.Verify(x => x.AllAsNoTracking(), Times.Once);
        }
Example #3
0
        public async Task <ActionResult> AddToFavourites(Favourite model)
        {
            var result = await favouritesService.AddAsync(model);

            if (result == 0)
            {
                return(Conflict());
            }

            return(Ok());
        }
        public async Task AddAsyncShouldWorkCorrectlyUsingMoq()
        {
            var favouriteProductsRepository = new Mock <IRepository <UserFavouriteProduct> >();
            var productsRepository          = new Mock <IDeletableEntityRepository <Product> >();

            var store       = new Mock <IUserStore <ApplicationUser> >();
            var userManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null);

            var productImages = new List <ProductImage>
            {
                new ProductImage {
                    ImageUrl = "TestImageUrl1"
                },
                new ProductImage {
                    ImageUrl = "TestImageUrl2"
                },
            };

            var productsList = new List <Product>
            {
                new Product {
                    Id = "TestProductId", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>()
                },
            };

            var favouriteProductsList = new List <UserFavouriteProduct>();

            favouriteProductsRepository.Setup(r => r.SaveChangesAsync()).Verifiable();
            favouriteProductsRepository.Setup(r => r.AddAsync(It.IsAny <UserFavouriteProduct>()))
            .Callback((UserFavouriteProduct item) => favouriteProductsList.Add(item));

            productsRepository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable());

            userManager.Setup(r => r.FindByIdAsync(It.IsAny <string>()))
            .Returns(async(string userId) => await Task.FromResult <ApplicationUser>(new ApplicationUser {
                Id = "TestUserId1"
            }));

            var service = new FavouritesService(favouriteProductsRepository.Object, productsRepository.Object, userManager.Object);

            Assert.True(await service.AddAsync("TestProductId", "TestUserId1"));
            Assert.Single(favouriteProductsList);
            Assert.Equal("TestProductId", favouriteProductsList.FirstOrDefault().ProductId);
            Assert.Equal("TestUserId1", favouriteProductsList.FirstOrDefault().UserId);

            productsRepository.Verify(x => x.AllAsNoTracking(), Times.Once);

            userManager.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once);

            favouriteProductsRepository.Verify(x => x.AddAsync(It.IsAny <UserFavouriteProduct>()), Times.Once);
            favouriteProductsRepository.Verify(x => x.SaveChangesAsync());
        }
        public async Task AddAsyncShouldWork()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddAsyncShouldWork").Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <Favourite>(dbContext);
            var service    = new FavouritesService(repository);

            await service.AddAsync(1, "test");

            Assert.True(repository.All().Any(x => x.PostId == 1 && x.UserId == "test"));
        }
        public async Task RemoveAsyncShouldWork()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "RemoveAsyncShouldWork").Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <Favourite>(dbContext);
            var service    = new FavouritesService(repository);

            await service.AddAsync(1, "test");

            Assert.Single(repository.All());
            await service.Remove(1, "test");

            Assert.Empty(repository.All());
        }
        public async Task AddAsyncShouldReturnFalseWhenUserIdDoesNotExistUsingMoq()
        {
            var favouriteProductsRepository = new Mock <IRepository <UserFavouriteProduct> >();
            var productsRepository          = new Mock <IDeletableEntityRepository <Product> >();

            var store       = new Mock <IUserStore <ApplicationUser> >();
            var userManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null);

            var productImages = new List <ProductImage>
            {
                new ProductImage {
                    ImageUrl = "TestImageUrl1"
                },
                new ProductImage {
                    ImageUrl = "TestImageUrl2"
                },
            };

            var productsList = new List <Product>
            {
                new Product {
                    Id = "TestProductId", Name = "TestProductName", Price = 42, Images = productImages, Reviews = new List <UserProductReview>()
                },
            };

            productsRepository.Setup(r => r.AllAsNoTracking()).Returns(productsList.AsQueryable());
            userManager.Setup(r => r.FindByIdAsync(It.IsAny <string>())).Returns(async(string userId) => await Task.FromResult <ApplicationUser>(null));

            var service = new FavouritesService(favouriteProductsRepository.Object, productsRepository.Object, userManager.Object);

            Assert.False(await service.AddAsync("TestProductId", "IvalidUserId"));

            productsRepository.Verify(x => x.AllAsNoTracking(), Times.Once);

            userManager.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once);
        }