public async Task CreateNewPizzaWithTakenName() { PizzaToCreateDto expectedPizzaToCreate = new PizzaToCreateDto { Name = "TestPizza", IngredientIds = new List <int> { 1 } }; pizzaRepoMock.Setup(x => x.GetByExpressionAsync(It.IsAny <Expression <Func <PizzaModel, bool> > >(), It.IsAny <Func <IQueryable <PizzaModel>, IIncludableQueryable <PizzaModel, object> > >())).Returns(GetPizzaByName(expectedPizzaToCreate.Name)); uowMock.Setup(x => x.Pizzas).Returns(pizzaRepoMock.Object); IPizzaService service = new PizzaService(uowMock.Object); IServiceResult <PizzaToReturnDto> creationResult = await service.CreateAsync(expectedPizzaToCreate); Assert.AreEqual(ResultType.Error, creationResult.Result); Assert.IsNull(creationResult.ReturnedObject); Assert.IsNotNull(creationResult.Errors); Assert.AreEqual(1, creationResult.Errors.Count); Assert.AreEqual($"Pizza name: {expectedPizzaToCreate.Name} is already taken", creationResult.Errors.First()); }
public async Task CreateNewPizzaCorrectly() { PizzaToCreateDto expectedPizzaToCreate = new PizzaToCreateDto { Name = "NewPizza", IngredientIds = new List <int> { 1 } }; // create mock of starter repository Mock <IFoodOrderRepository <StarterModel> > starterRepoMock = new Mock <IFoodOrderRepository <StarterModel> >(); starterRepoMock.Setup(x => x.GetByExpressionAsync(It.IsAny <Expression <Func <StarterModel, bool> > >(), It.IsAny <Func <IQueryable <StarterModel>, IIncludableQueryable <StarterModel, object> > >())).Returns(GetExpectedStarters()); 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(expectedPizzaToCreate.IngredientIds[0])); pizzaRepoMock.Setup(x => x.CreateAsync(It.IsAny <PizzaModel>())).Returns(Task.FromResult(true)); uowMock.Setup(x => x.Pizzas).Returns(pizzaRepoMock.Object); uowMock.Setup(x => x.Starters).Returns(starterRepoMock.Object); uowMock.Setup(x => x.Ingredients).Returns(ingredientRepoMock.Object); IPizzaService service = new PizzaService(uowMock.Object); IServiceResult <PizzaToReturnDto> creationResult = await service.CreateAsync(expectedPizzaToCreate); Assert.AreEqual(ResultType.Created, creationResult.Result); Assert.IsNotNull(creationResult.ReturnedObject); Assert.AreEqual(expectedPizzaToCreate.Name, creationResult.ReturnedObject.Name); Assert.AreEqual("sos", creationResult.ReturnedObject.Ingredients.First()); Assert.IsNotNull(creationResult.ReturnedObject.TotalPrices); Assert.AreEqual(4, creationResult.ReturnedObject.TotalPrices.Count); }