コード例 #1
0
        public async Task AddIngredientToPizzaCorrectly()
        {
            string expectedPizzaName      = "TestPizza";
            int    expectedIdOfIngredient = 3;

            pizzaRepoMock.Setup(x => x.GetByExpressionAsync(It.IsAny <Expression <Func <PizzaModel, bool> > >(),
                                                            It.IsAny <Func <IQueryable <PizzaModel>, IIncludableQueryable <PizzaModel, object> > >())).Returns(GetPizzaByName(expectedPizzaName));

            pizzaRepoMock.Setup(x => x.Update(It.IsAny <PizzaModel>()));

            Mock <IFoodOrderRepository <IngredientModel> > ingredientRepoMock = new Mock <IFoodOrderRepository <IngredientModel> >();

            ingredientRepoMock.Setup(x => x.GetByExpressionAsync(It.IsAny <Expression <Func <IngredientModel, bool> > >(),
                                                                 It.IsAny <Func <IQueryable <IngredientModel>, IIncludableQueryable <IngredientModel, object> > >())).Returns(GetExpectedIngredientById(expectedIdOfIngredient));

            uowMock.Setup(x => x.Pizzas).Returns(pizzaRepoMock.Object);
            uowMock.Setup(x => x.Ingredients).Returns(ingredientRepoMock.Object);
            uowMock.Setup(x => x.SaveChangesAsync());

            IPizzaService service = new PizzaService(uowMock.Object);

            IServiceResult <PizzaToReturnDto> result = await service.AddIngredientAsync(expectedPizzaName, expectedIdOfIngredient);

            Assert.AreEqual(ResultType.Edited, result.Result);
            Assert.IsNull(result.Errors);
            Assert.AreEqual(6, result.ReturnedObject.Ingredients.Count);

            for (int i = 0; i < result.ReturnedObject.TotalPrices.Count; i++)
            {
                Assert.AreEqual(expectedPizzas.First().PizzaDetails.ElementAt(i).TotalPrice, result.ReturnedObject.TotalPrices.ElementAt(i).Price);
            }
        }
コード例 #2
0
        public async Task AddIngredientToPizzaThatIsAlreadyIncluded()
        {
            string expectedPizzaName      = "TestPizza";
            int    expectedIdOfIngredient = 2;

            pizzaRepoMock.Setup(x => x.GetByExpressionAsync(It.IsAny <Expression <Func <PizzaModel, bool> > >(),
                                                            It.IsAny <Func <IQueryable <PizzaModel>, IIncludableQueryable <PizzaModel, object> > >())).Returns(GetPizzaByName(expectedPizzaName));

            uowMock.Setup(x => x.Pizzas).Returns(pizzaRepoMock.Object);

            IPizzaService service = new PizzaService(uowMock.Object);

            IServiceResult <PizzaToReturnDto> result = await service.AddIngredientAsync(expectedPizzaName, expectedIdOfIngredient);

            Assert.AreEqual(ResultType.Error, result.Result);
            Assert.IsNull(result.ReturnedObject);
            Assert.IsNotNull(result.Errors);
            Assert.AreEqual($"Error during addition of new ingredient to {expectedPizzaName} pizza", result.Errors.First());
        }