Beispiel #1
0
        public void ReturnZero_IfPizzaWasNotSuccessfullyAddedToCart_WhenCalled()
        {
            // Arrange
            Guid userId = Guid.NewGuid();

            var user = new ApplicationUser()
            {
                Cart = new List <BasePizza>()
            };
            IEnumerable <ApplicationUser> users = new List <ApplicationUser>()
            {
                user
            };
            IEnumerable <Pizza>       pizzas       = Helper.GetPizzas();
            IEnumerable <CustomPizza> customPizzas = Helper.GetCustomPizzas();

            var pizzaContextMock = new Mock <IPizzaFactoryDbContext>();
            var orderContextMock = new Mock <IOrderDbContext>();
            var userContextMock  = new Mock <IIdentityDbContext>()
            {
                CallBase = true
            };
            var mapperMock    = new Mock <IMapper>();
            var validatorMock = new Mock <IValidator>();

            var pizzaDbSetMock       = QueryableDbSetMock.GetQueryableMockDbSet(pizzas);
            var customPizzaDbSetMock = QueryableDbSetMock.GetQueryableMockDbSet(customPizzas);
            var userDbSetMock        = QueryableDbSetMock.GetQueryableMockDbSet(users);

            userDbSetMock.Setup(u => u.Find(userId.ToString())).Returns(user);
            userContextMock.Setup(ctx => ctx.Users).Returns(userDbSetMock.Object);
            userContextMock.Setup(ctx => ctx.SaveChanges()).Returns(() => 0);
            pizzaContextMock.Setup(ctx => ctx.Pizzas).Returns(pizzaDbSetMock.Object);
            pizzaContextMock.Setup(ctx => ctx.CustomPizzas).Returns(customPizzaDbSetMock.Object);

            IApplicationUserService userService =
                new ApplicationUserService(userContextMock.Object,
                                           pizzaContextMock.Object,
                                           orderContextMock.Object,
                                           mapperMock.Object,
                                           validatorMock.Object);

            // Act
            int result = userService.AddToCart(userId.ToString(), pizzas.First().Id);

            // Assert
            Assert.AreEqual(0, result);
        }
Beispiel #2
0
        public void CallMapperFromPizzaAndCustomPizza_WhenCalled()
        {
            // Arrange
            Guid userId = Guid.NewGuid();

            var user = new ApplicationUser()
            {
                Cart = new List <BasePizza>()
            };
            IEnumerable <ApplicationUser> users = new List <ApplicationUser>()
            {
                user
            };
            IEnumerable <Pizza>       pizzas       = Helper.GetPizzas();
            IEnumerable <CustomPizza> customPizzas = Helper.GetCustomPizzas();

            var pizzaContextMock = new Mock <IPizzaFactoryDbContext>();
            var orderContextMock = new Mock <IOrderDbContext>();
            var userContextMock  = new Mock <IIdentityDbContext>()
            {
                CallBase = true
            };
            var mapperMock    = new Mock <IMapper>();
            var validatorMock = new Mock <IValidator>();

            var pizzaDbSetMock       = QueryableDbSetMock.GetQueryableMockDbSet(pizzas);
            var customPizzaDbSetMock = QueryableDbSetMock.GetQueryableMockDbSet(customPizzas);
            var userDbSetMock        = QueryableDbSetMock.GetQueryableMockDbSet(users);

            userDbSetMock.Setup(u => u.Find(userId.ToString())).Returns(user);
            userContextMock.Setup(ctx => ctx.Users).Returns(userDbSetMock.Object);
            pizzaContextMock.Setup(ctx => ctx.Pizzas).Returns(pizzaDbSetMock.Object);
            pizzaContextMock.Setup(ctx => ctx.CustomPizzas).Returns(customPizzaDbSetMock.Object);

            IApplicationUserService userService =
                new ApplicationUserService(userContextMock.Object,
                                           pizzaContextMock.Object,
                                           orderContextMock.Object,
                                           mapperMock.Object,
                                           validatorMock.Object);

            // Act
            userService.AddToCart(userId.ToString(), pizzas.First().Id);

            // Assert
            mapperMock.Verify(v => v.FromPizzaAndCustomPizza(It.IsAny <Pizza>(), It.IsAny <CustomPizza>()), Times.Once);
        }