public void Can_Delete_Valid_Rules() { // Arrange - create a Product MT_LOAD_RULE loadRule = new MT_LOAD_RULE { LoadRuleId = 2, Code = "Test" }; // Arrange - create the mock repository Mock <IRuleRepository> mock = new Mock <IRuleRepository>(); mock.Setup(m => m.LoadRules).Returns(new MT_LOAD_RULE[] { new MT_LOAD_RULE { LoadRuleId = 1, Code = "LR1" }, loadRule, new MT_LOAD_RULE { LoadRuleId = 3, Code = "LR3" }, }.AsQueryable()); // Arrange - create the controller RuleController target = new RuleController(mock.Object); // Act - delete the product target.RuleDelete(loadRule.LoadRuleId); // Assert - ensure that the repository delete method was // called with the correct Product mock.Verify(m => m.DeleteLoadRule(loadRule.LoadRuleId)); }
public void Can_Edit_Rule() { // Arrange - create the mock repository Mock <IRuleRepository> mock = new Mock <IRuleRepository>(); mock.Setup(m => m.LoadRules).Returns(new MT_LOAD_RULE[] { new MT_LOAD_RULE { LoadRuleId = 1, Code = "LR1" }, new MT_LOAD_RULE { LoadRuleId = 2, Code = "LR2" }, new MT_LOAD_RULE { LoadRuleId = 3, Code = "LR3" }, }.AsQueryable()); // Arrange - create the controller RuleController target = new RuleController(mock.Object); // Act MT_LOAD_RULE p1 = target.RuleEdit(1).ViewData.Model as MT_LOAD_RULE; MT_LOAD_RULE p2 = target.RuleEdit(2).ViewData.Model as MT_LOAD_RULE; MT_LOAD_RULE p3 = target.RuleEdit(3).ViewData.Model as MT_LOAD_RULE; // Assert Assert.AreEqual(1, p1.LoadRuleId); Assert.AreEqual(2, p2.LoadRuleId); Assert.AreEqual(3, p3.LoadRuleId); }
public ActionResult RuleDelete(long loadRuleId) { MT_LOAD_RULE deletedLoadRule = repository.DeleteLoadRule(loadRuleId); if (deletedLoadRule != null) { TempData["message"] = string.Format("{0} был удален", deletedLoadRule.Code); } return(RedirectToAction("Rules")); }
public MT_LOAD_RULE DeleteLoadRule(long loadRuleId) { MT_LOAD_RULE dbEntry = context.MT_LOAD_RULE.Where(x => x.LoadRuleId == loadRuleId).Single(); //.Find(loadRuleId); if (dbEntry != null) { context.MT_LOAD_RULE.Remove(dbEntry); context.SaveChanges(); } return(dbEntry); }
public ActionResult RuleEdit(MT_LOAD_RULE loadRule) { if (ModelState.IsValid) { repository.SaveLoadRule(loadRule); TempData["message"] = string.Format("{0} сохранено", loadRule.Code); return(RedirectToAction("Rules")); } else { // что-то не так с значениями данных (there is something wrong with the data values) return(View(loadRule)); } }
public void Can_Save_Valid_Changes() { // Arrange - create mock repository Mock <IRuleRepository> mock = new Mock <IRuleRepository>(); // Arrange - create the controller RuleController target = new RuleController(mock.Object); // Arrange - create a loadRule MT_LOAD_RULE loadRule = new MT_LOAD_RULE { Code = "Test" }; // Act - try to save the loadRule ActionResult result = target.RuleEdit(loadRule); // Assert - check that the repository was called mock.Verify(m => m.SaveLoadRule(loadRule)); // Assert - check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void SaveLoadRule(MT_LOAD_RULE loadRule) { if (loadRule.LoadRuleId == 0) { context.MT_LOAD_RULE.Add(loadRule); } else { MT_LOAD_RULE dbEntry = context.MT_LOAD_RULE.Where(x => x.LoadRuleId == loadRule.LoadRuleId).Single(); //.Find(loadRule.LoadRuleId); if (dbEntry != null) { dbEntry.Code = loadRule.Code; dbEntry.Pattern = loadRule.Pattern; dbEntry.Method = loadRule.Method; dbEntry.Descr = loadRule.Descr; dbEntry.IsActive = loadRule.IsActive; dbEntry.Ord = loadRule.Ord; } } context.SaveChanges(); }
public void Cannot_Save_Invalid_Changes() { // Arrange - create mock repository Mock <IRuleRepository> mock = new Mock <IRuleRepository>(); // Arrange - create the controller RuleController target = new RuleController(mock.Object); // Arrange - create a loadRule MT_LOAD_RULE loadRule = new MT_LOAD_RULE { Code = "Test" }; // Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - try to save the loadRule ActionResult result = target.RuleEdit(loadRule); // Assert - check that the repository was not called mock.Verify(m => m.SaveLoadRule(It.IsAny <MT_LOAD_RULE>()), Times.Never()); // Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Cannot_Edit_Nonexistent_Rule() { // Arrange - create the mock repository Mock <IRuleRepository> mock = new Mock <IRuleRepository>(); mock.Setup(m => m.LoadRules).Returns(new MT_LOAD_RULE[] { new MT_LOAD_RULE { LoadRuleId = 1, Code = "LR1" }, new MT_LOAD_RULE { LoadRuleId = 2, Code = "LR2" }, new MT_LOAD_RULE { LoadRuleId = 3, Code = "LR3" }, }.AsQueryable()); // Arrange - create the controller RuleController target = new RuleController(mock.Object); // Act MT_LOAD_RULE result = (MT_LOAD_RULE)target.RuleEdit(4).ViewData.Model; // Assert Assert.IsNull(result); }
public ViewResult RuleEdit(long loadRuleId) { MT_LOAD_RULE loadRule = repository.LoadRules.Where(p => p.LoadRuleId == loadRuleId).FirstOrDefault(); //.FirstOrDefault(p => p.LoadRuleId == loadRuleId); return(View(loadRule)); }