public void AddsUserIdToEntity() { // Act. BudgetsController controller = new BudgetsController(this.mockBudgetsRepository.Object); IActionResult result = controller.Post(this.budget); OkObjectResult okResult = result as OkObjectResult; // Assert. this.mockBudgetsRepository.Verify(m => m.Add(It.Is <Budget>(x => x.UserId == this.userId), It.IsAny <long>()), Times.Once, "The user ID should have been added to the entity."); }
public void ReturnsRepositoryResult() { // Act. BudgetsController controller = new BudgetsController(this.mockBudgetsRepository.Object); IActionResult result = controller.Post(this.budget); OkObjectResult okResult = result as OkObjectResult; // Assert. this.mockBudgetsRepository.Verify(m => m.Add(this.budget, this.userId), Times.Once, "The add method should have been called."); this.mockBudgetsRepository.VerifyNoOtherCalls(); Assert.IsNotNull(okResult, "An OK response should have been returned."); Assert.AreEqual(200, okResult.StatusCode, "The status code from the response should have been 200."); Assert.AreEqual(this.stubBudget, okResult.Value, "The result from the repository should have been returned."); }
public void HandlesGeneralException() { // Arrange. this.mockBudgetsRepository .Setup(m => m.Add(It.IsAny <Budget>(), It.IsAny <long>())) .Throws(new Exception()); // Act. BudgetsController controller = new BudgetsController(this.mockBudgetsRepository.Object); IActionResult result = controller.Post(this.budget); ObjectResult objectResult = result as ObjectResult; // Assert. Assert.IsNotNull(objectResult, "An object result should have been returned."); Assert.AreEqual(500, objectResult.StatusCode, "The status code from the response should have been 500."); string expectedMessage = "There was an error saving the budget."; Assert.AreEqual(expectedMessage, objectResult.Value, "The error message should have been the result."); }
public void ReturnsBadRequestErrorWhenBudgetNull() { // Arrange. this.budget = null; // Act. BudgetsController controller = new BudgetsController(this.mockBudgetsRepository.Object); IActionResult result = controller.Post(this.budget); BadRequestObjectResult badRequestResult = result as BadRequestObjectResult; // Assert. this.mockBudgetsRepository.VerifyNoOtherCalls(); Assert.IsNotNull(badRequestResult, "A bad request response should have been returned."); Assert.AreEqual(400, badRequestResult.StatusCode, "The status code from the response should have been 405."); string expectedMessage = "A budget must be passed in for it to be saved."; Assert.AreEqual(expectedMessage, badRequestResult.Value, "The error message should have been the result."); }