Ejemplo n.º 1
0
        public async Task AddingSameProductToWishListShouldCallDb()
        {
            var userRepo     = new Mock <IRepository <User> >();
            var productsRepo = new Mock <IRepository <Product> >();

            userRepo.Setup(r => r.All()).Returns(new List <User>
            {
                new User
                {
                    Id             = "1",
                    UserName       = "******",
                    PurchaseOrders = new List <Order>(),
                    Reports        = new List <Report>(),
                    MyProducts     = new List <Product>(),
                    Whishlist      = ""
                },
                new User
                {
                    Id             = "2",
                    UserName       = "******",
                    PurchaseOrders = new List <Order>(),
                    Reports        = new List <Report>(),
                    MyProducts     = new List <Product>()
                },
                new User
                {
                    Id             = "3",
                    UserName       = "******",
                    PurchaseOrders = new List <Order>(),
                    Reports        = new List <Report>(),
                    MyProducts     = new List <Product>()
                }
            }.AsQueryable());

            var productId = Guid.NewGuid();

            productsRepo.Setup(r => r.All()).Returns(new List <Product>
            {
                new Product()
                {
                    Id = productId
                },
                new Product()
                {
                    Id = Guid.NewGuid()
                },
            }.AsQueryable());

            var service = new UsersService(userRepo.Object, null, productsRepo.Object);

            await service.AddProductToWhishlist(productId.ToString(), "stamat");

            await service.AddProductToWhishlist(productId.ToString(), "stamat");

            var wishlist = service.GetWishList("stamat");

            Assert.Equal(1, wishlist.Count);
            Assert.Contains(wishlist, p => p.Id == productId);
            userRepo.Verify(r => r.SaveChangesAsync(), Times.Once);
        }
Ejemplo n.º 2
0
        public async Task AddToWishListShouldThrowIfUserDoesNotExist()
        {
            var userRepo = new Mock <IRepository <User> >();

            userRepo.Setup(r => r.All()).Returns(new List <User>
            {
                new User
                {
                    Id             = "1",
                    UserName       = "******",
                    PurchaseOrders = new List <Order>(),
                    Reports        = new List <Report>(),
                    MyProducts     = new List <Product>()
                },
                new User
                {
                    Id             = "2",
                    UserName       = "******",
                    PurchaseOrders = new List <Order>(),
                    Reports        = new List <Report>(),
                    MyProducts     = new List <Product>()
                },
                new User
                {
                    Id             = "3",
                    UserName       = "******",
                    PurchaseOrders = new List <Order>(),
                    Reports        = new List <Report>(),
                    MyProducts     = new List <Product>()
                }
            }.AsQueryable());

            var service = new UsersService(userRepo.Object, null, null);

            await Assert.ThrowsAsync <NullReferenceException>(async() => await service.AddProductToWhishlist("1", "nqkuv"));

            userRepo.Verify(r => r.SaveChangesAsync(), Times.Never);
        }