public void Delete_Expense_ReturnsPartial() { // Arrange ExpenseController controller = new ExpenseController(commandBus.Object, categoryRepository.Object, expenseRepository.Object); commandBus.Setup(c => c.Submit(It.IsAny<DeleteExpenseCommand>())).Returns(new CommandResult(true)); IEnumerable<Expense> fakeExpenses = GetMockExpenses(); expenseRepository.Setup(x => x.GetMany(It.IsAny<Expression<Func<Expense, bool>>>())).Returns(fakeExpenses); // Act var result = controller.Delete(1) as PartialViewResult; // Assert Assert.IsInstanceOf(typeof(IEnumerable<Expense>), result.ViewData.Model, "Wrong View Model"); Assert.AreEqual("_ExpenseList", result.ViewName); }
public void Cannot_Create_Empty_Expense() { // Arrange ExpenseController controller = new ExpenseController(commandBus.Object, categoryRepository.Object, expenseRepository.Object); // The MVC pipeline doesn't run, so binding and validation don't run. controller.ModelState.AddModelError("", "mock error message"); Mapper.CreateMap<ExpenseFormModel, CreateOrUpdateExpenseCommand>(); // Act ExpenseFormModel expense = new ExpenseFormModel(); expense.ExpenseId = 0; var result = controller.Save(expense) as ViewResult; // Assert - check that we are passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); Assert.AreEqual("Create", result.ViewName); }
public void Create_Expense_Redirects_To_Index() { // Arrange ExpenseController controller = new ExpenseController(commandBus.Object, categoryRepository.Object, expenseRepository.Object); commandBus.Setup(c => c.Submit(It.IsAny<CreateOrUpdateExpenseCommand>())).Returns(new CommandResult(true)); Mapper.CreateMap<ExpenseFormModel, CreateOrUpdateExpenseCommand>(); // Act ExpenseFormModel expense = new ExpenseFormModel { Transaction = "Mock Transaction", Date = DateTime.Now, Amount = 1000, CategoryId = 1 }; var result = controller.Save(expense) as RedirectToRouteResult; // Assert Assert.AreEqual("Index", result.RouteValues["action"]); }
public void Index_AjaxRequest_Returns_Partial_With_Expense_List() { // Arrange Mock<HttpRequestBase> request = new Mock<HttpRequestBase>(); Mock<HttpResponseBase> response = new Mock<HttpResponseBase>(); Mock<HttpContextBase> context = new Mock<HttpContextBase>(); context.Setup(c => c.Request).Returns(request.Object); context.Setup(c => c.Response).Returns(response.Object); request.Setup(req => req["X-Requested-With"]).Returns("XMLHttpRequest"); IEnumerable<Expense> fakeExpenses = GetMockExpenses(); expenseRepository.Setup(x => x.GetMany(It.IsAny<Expression<Func<Expense, bool>>>())).Returns(fakeExpenses); ExpenseController controller = new ExpenseController(commandBus.Object, categoryRepository.Object,expenseRepository.Object); controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); // Act var result = controller.Index(null, null) as PartialViewResult; // Assert Assert.IsNotNull(result, "View Result is null"); Assert.AreEqual("_ExpenseList", result.ViewName); Assert.IsInstanceOf(typeof(IEnumerable<Expense>), result.ViewData.Model, "Wrong View Model"); var expenses = result.ViewData.Model as IEnumerable<Expense>; Assert.AreEqual(3, expenses.Count(), "Got wrong number of Categories"); }