public void Show_Orders_Without_Filters() { Guid custId = Guid.NewGuid(); Order[] orders = new Order[] { new Order() { CustomerID = Guid.NewGuid() }, new Order() { CustomerID = custId }, new Order() { CustomerID = custId }, }; UnitOfWorkMock.Setup(m => m.Orders.GetAllItems()).Returns(orders); UserManagerMock.Setup(m => m.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(custId.ToString()); var target = new OrderController(UnitOfWorkMock.Object, UserManagerMock.Object); var resultModel = (target.Index(new OrderViewModel()) as ViewResult).Model as OrderViewModel; Assert.Equal(2, resultModel.Orders.Count()); Assert.True(resultModel.Orders.First().CustomerID == custId); }
public void Show_Orders_With_Some_Filter() { Guid custId = Guid.NewGuid(); Order[] orders = new Order[] { new Order() { CustomerID = Guid.NewGuid(), StatusID = 1 }, new Order() { CustomerID = custId, StatusID = 2 }, new Order() { CustomerID = custId, StatusID = 3 }, }; UnitOfWorkMock.Setup(m => m.Orders.GetAllItems()).Returns(orders); UserManagerMock.Setup(m => m.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(custId.ToString()); var target = new OrderController(UnitOfWorkMock.Object, UserManagerMock.Object); var resultModel = (target.Index(new OrderViewModel() { Status = Statuses.InProgress }) as ViewResult).Model as OrderViewModel; Assert.Single(resultModel.Orders); Assert.True(resultModel.Orders.First().StatusID == 2); }
public async Task Update_User() { UserManagerMock.Setup(m => m.FindByEmailAsync(It.IsAny <string>())) .ReturnsAsync(new IdentityUser()); UserManagerMock.Setup(m => m.UpdateAsync(It.IsAny <IdentityUser>())) .ReturnsAsync(IdentityResult.Success); UserValidatorMock.Setup(m => m.ValidateAsync(UserManagerMock.Object, It.IsAny <IdentityUser>())) .ReturnsAsync(IdentityResult.Success); UnitOfWorkMock.Setup(um => um.Customers.UpdateItem(It.IsAny <Customer>())).Verifiable(); UnitOfWorkMock.Setup(um => um.SaveChanges()).Verifiable(); var target = new UserController(UserManagerMock.Object, null, null, UserValidatorMock.Object, UnitOfWorkMock.Object, null); var resultAction = await target.Create(new UserInfoViewModel() { Customer = new Customer(), Email = "*****@*****.**", Password = "******", IsAdmin = false }) as RedirectToActionResult; Assert.NotNull(resultAction); UnitOfWorkMock.Verify(e => e.SaveChanges()); UnitOfWorkMock.Verify(e => e.Customers.UpdateItem(It.IsAny <Customer>())); }
public void Show_All_Products() { var products = new Product[] { new Product() { Name = "Name1" }, new Product() { Name = "Name4" }, new Product() { Name = "Name8" }, new Product() { Name = "Name3" }, }; UnitOfWorkMock.Setup(m => m.Products.GetAllItems()).Returns(products); var target = new ProductController(UnitOfWorkMock.Object, Cart); var resultModel = ((target.Index() as ViewResult)?.Model as IEnumerable <Product>).ToArray(); Assert.True(resultModel[0].Name == "Name1"); Assert.True(resultModel[1].Name == "Name4"); Assert.True(resultModel[2].Name == "Name8"); Assert.True(resultModel[3].Name == "Name3"); }
public async Task Edit_Not_Admin() { IdentityUser[] users = new IdentityUser[] { new IdentityUser("User1") { Id = "9E922BD2-4395-4524-8264-4F218D600E9D", Email = "*****@*****.**" }, new IdentityUser("User2") { Id = "4192883F-AA46-42EA-B1DD-73E8CAA7A7B3", Email = "*****@*****.**" } }; UnitOfWorkMock.Setup(m => m.Customers.GetItem(It.IsAny <Guid>())) .Returns(new Customer() { Name = "NameCustomer" }); UserManagerMock.Setup(m => m.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(users[0]); UserManagerMock.Setup(m => m.IsInRoleAsync(It.IsAny <IdentityUser>(), "Admin")).ReturnsAsync(false); var target = new UserController(UserManagerMock.Object, null, null, null, UnitOfWorkMock.Object, null); var resultAction = await target.Edit(Guid.Parse(users[0].Id)); var resultModel = (resultAction as ViewResult).Model as UserInfoViewModel; Assert.True(resultModel.IsAdmin == false); Assert.True(resultModel.Customer.Name == "NameCustomer"); Assert.True(resultModel.Email == "*****@*****.**"); }
public void Delete_Non_Existence_Product() { UnitOfWorkMock.Setup(m => m.Products.GetItem(It.IsAny <Guid>())).Returns(value: null); UnitOfWorkMock.Setup(m => m.Products.DeleteItem(It.IsAny <Product>())).Verifiable(); var target = new ProductController(UnitOfWorkMock.Object, Cart); target.Delete(It.IsAny <Guid>()); UnitOfWorkMock.Verify(v => v.Products.DeleteItem(It.IsAny <Product>()), Times.Never); }
public void Edit_Product() { UnitOfWorkMock.Setup(m => m.Products.GetItem(It.IsAny <Guid>())).Returns(new Product() { Name = "Name" }); var target = new ProductController(UnitOfWorkMock.Object, Cart); var result = (target.Edit(It.IsAny <Guid>()) as ViewResult); Assert.Equal("Редактирование продукта", result.ViewData["Action"]); Assert.Equal("Name", (result.Model as Product)?.Name); }
public void Invalid_Delete_Order() { UnitOfWorkMock.Setup(m => m.Orders.GetItem(It.IsAny <Guid>())).Returns(new Order() { StatusID = 2 }); var target = new OrderController(UnitOfWorkMock.Object, UserManagerMock.Object); var result = target.Delete(It.IsAny <Guid>(), It.IsAny <Statuses>()) as ViewResult; Assert.NotNull(result); UnitOfWorkMock.Verify(uow => uow.Orders.DeleteItem(It.IsAny <Order>()), Times.Never); }
public void Create_Valid_Product() { UnitOfWorkMock.Setup(m => m.Products.GetAllItems()).Returns(new List <Product>()); UnitOfWorkMock.Setup(m => m.SaveChanges()).Verifiable(); UnitOfWorkMock.Setup(m => m.Products.AddItem(It.IsAny <Product>())).Verifiable(); var target = new ProductController(UnitOfWorkMock.Object, Cart); var result = (target.Create(new Product())) as RedirectToActionResult; Assert.NotNull(result); UnitOfWorkMock.Verify(v => v.SaveChanges()); UnitOfWorkMock.Verify(v => v.Products.AddItem(It.IsAny <Product>())); }
public void Valid_Delete_Order() { UnitOfWorkMock.Setup(m => m.Orders.GetItem(It.IsAny <Guid>())).Returns(new Order() { StatusID = 3 }); UnitOfWorkMock.Setup(m => m.Orders.DeleteItem(It.IsAny <Order>())).Verifiable(); var target = new OrderController(UnitOfWorkMock.Object, UserManagerMock.Object); var result = target.Delete(It.IsAny <Guid>(), It.IsAny <Statuses>()) as RedirectToActionResult; Assert.NotNull(result); UnitOfWorkMock.Verify(uow => uow.Orders.DeleteItem(It.IsAny <Order>())); }
public void Delete_Real_Product() { Product testProduct = new Product() { ID = Guid.Parse("BFF5192C-14F0-4FD7-BCB9-B1EE32737301") }; UnitOfWorkMock.Setup(m => m.Products.GetItem(It.IsAny <Guid>())).Returns(testProduct); UnitOfWorkMock.Setup(m => m.Products.DeleteItem(It.IsAny <Product>())).Verifiable(); var target = new ProductController(UnitOfWorkMock.Object, Cart); target.Delete(testProduct.ID); UnitOfWorkMock.Verify(v => v.Products.DeleteItem(It.IsAny <Product>()), Times.Once); }
public void Show_One_Order_Info() { var orderMock = new Mock <Order>(); Order myOrder = orderMock.Object; myOrder.OrderNumber = 3; Order[] orders = new Order[] { new Order() { OrderNumber = 1 }, myOrder, new Order() { OrderNumber = 2 } }; myOrder.Products.Add(new OrderProduct() { Product = new Product() { Price = 100 }, ProductCount = 1 }); myOrder.Products.Add(new OrderProduct() { Product = new Product() { Price = 200 }, ProductCount = 2 }); UserManagerMock.Setup(usm => usm.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(Guid.NewGuid().ToString()); UnitOfWorkMock.Setup(unm => unm.Orders.GetAllItems()).Returns(orders); UnitOfWorkMock.Setup(unm => unm.Customers.GetItem(It.IsAny <Guid>())).Returns(new Customer() { Discount = 10 }); var targer = new OrderController(UnitOfWorkMock.Object, UserManagerMock.Object); var resultModel = (targer.FullInfo(orderMock.Object.OrderNumber) as ViewResult) .Model as AloneOrderInfoViewModel; Assert.Same(myOrder, resultModel.Order); Assert.Equal(450, resultModel.TotalPrice); }
public void Make_Valid_Order() { var cart = new SessionCart(); cart.AddItem(new Product(), 3); UnitOfWorkMock.Setup(m => m.Orders.GetAllItems()).Returns(Enumerable.Empty <Order>()); UnitOfWorkMock.Setup(m => m.Orders.AddItem(It.IsAny <Order>())).Verifiable(); UnitOfWorkMock.Setup(m => m.SaveChanges()).Verifiable(); UserManagerMock.Setup(m => m.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(Guid.NewGuid().ToString()); var target = new CartController(UnitOfWorkMock.Object, cart, UserManagerMock.Object); var result = target.MakeOrder("returnUrl") as RedirectResult; UnitOfWorkMock.Verify(m => m.SaveChanges()); UnitOfWorkMock.Verify(m => m.Orders.AddItem(It.IsAny <Order>())); Assert.Equal("returnUrl", result.Url); }
public void should_handle_create_employee_command() { // Arrange var inputCommand = new EmployeeCreateCommand("joe", "bedford", "*****@*****.**"); EmployeeRepositoryMock .Setup(i => i.CreateAsync(It.IsAny <Employee>())) .Returns(Task.CompletedTask); UnitOfWorkMock .Setup(i => i.CommitAsync(default(CancellationToken))) .ReturnsAsync(1); // Act var result = EmployeeCreateCommandHandlerUnderTest.Handle(inputCommand, default(CancellationToken)).Result; // Assert Assert.NotEqual(Guid.Empty, result.Id); }
public void Update_Product() { Product testProduct = new Product() { Name = "Name1", ID = Guid.Parse("AD65CE6B-10C5-463E-B0E8-61B08A2BE7A8") }; UnitOfWorkMock.Setup(m => m.Products.GetAllItems()).Returns(new List <Product>() { testProduct }); UnitOfWorkMock.Setup(m => m.SaveChanges()).Verifiable(); UnitOfWorkMock.Setup(m => m.Products.UpdateItem(It.IsAny <Product>())).Verifiable(); var target = new ProductController(UnitOfWorkMock.Object, Cart); var result = (target.Create(testProduct)) as RedirectToActionResult; Assert.NotNull(result); UnitOfWorkMock.Verify(v => v.SaveChanges()); UnitOfWorkMock.Verify(v => v.Products.UpdateItem(It.IsAny <Product>())); }
public void Setup() { var reqDefId = 0; _modeRepository = new ModeRepository(_plantProvider, _sourceModes); _sourceModes.Add(new Mode(_sourcePlant, "ModeA", false)); _sourceModes.Add(new Mode(_sourcePlant, "ModeB", false)); _requirementTypeRepository = new RequirementTypeRepository(_plantProvider, _sourceRequirementTypes); var requirementTypeA = new RequirementType(_sourcePlant, "RequirementTypeCodeA", "RequirementTypeTitleA", _requirementIconOther, 1); var reqDefA1 = new RequirementDefinition(_sourcePlant, "RequirementDefCodeA1", 1, RequirementUsage.ForAll, 2); reqDefA1.SetProtectedIdForTesting(++reqDefId); requirementTypeA.AddRequirementDefinition(reqDefA1); var reqDefA2 = new RequirementDefinition(_sourcePlant, "RequirementDefCodeA2", 3, RequirementUsage.ForAll, 4); reqDefA2.SetProtectedIdForTesting(++reqDefId); requirementTypeA.AddRequirementDefinition(reqDefA2); reqDefA1.AddField(new Field(_sourcePlant, "LabelA", FieldType.Number, 1, "UnitA", true)); var requirementTypeB = new RequirementType(_sourcePlant, "RequirementTypeCodeB", "RequirementTypeTitleB", _requirementIconOther, 2); _sourceRequirementTypes.Add(requirementTypeA); _sourceRequirementTypes.Add(requirementTypeB); _tagFunctionRepository = new TagFunctionRepository(_plantProvider, _sourceTagFunctions); var tagFunctionA = new TagFunction(_sourcePlant, _tfCodeA, "TagFunctionDescA", _regCodeA); tagFunctionA.AddRequirement(new TagFunctionRequirement(_sourcePlant, 1, reqDefA1)); tagFunctionA.AddRequirement(new TagFunctionRequirement(_sourcePlant, 2, reqDefA2)); var tagFunctionB = new TagFunction(_sourcePlant, _tfCodeB, "TagFunctionDescB", _regCodeB); _sourceTagFunctions.Add(tagFunctionA); _sourceTagFunctions.Add(tagFunctionB); _tagFunctionApiServiceMock = new Mock <ITagFunctionApiService>(); _tagFunctionApiServiceMock .Setup(t => t.TryGetTagFunctionAsync(TestPlant, _tfCodeA, _regCodeA)) .Returns(Task.FromResult(new PCSTagFunction { Code = _tfCodeA, RegisterCode = _regCodeA })); _tagFunctionApiServiceMock .Setup(t => t.TryGetTagFunctionAsync(TestPlant, _tfCodeB, _regCodeB)) .Returns(Task.FromResult(new PCSTagFunction { Code = _tfCodeB, RegisterCode = _regCodeB })); _command = new CloneCommand(_sourcePlant, TestPlant); _dut = new CloneCommandHandler( _plantProvider, _plantProvider, UnitOfWorkMock.Object, _modeRepository, _requirementTypeRepository, _tagFunctionRepository, _tagFunctionApiServiceMock.Object); UnitOfWorkMock .Setup(uiw => uiw.SaveChangesAsync(It.IsAny <CancellationToken>())) .Callback(() => { // Need this to simulate what EF Core do with Ids upon saving new Items _requirementTypeRepository.Save(); }); }