コード例 #1
0
        public void BudgetsController_Get_ReturnAllElementsInRepo_WhenGivenNoParameters()
        {
            var result             = controller.Get();
            var resultObjectResult = result as ObjectResult;
            var resultObject       = resultObjectResult.Value as IEnumerable <Budget>;

            Assert.IsType <ObjectResult>(result);
            Assert.Equal(3, resultObject.Count());
        }
コード例 #2
0
            public void ReturnsRepositoryResult()
            {
                // Act.
                BudgetsController controller = new BudgetsController(this.mockBudgetsRepository.Object);
                IActionResult     result     = controller.Get();
                OkObjectResult    okResult   = result as OkObjectResult;

                // Assert.
                this.mockBudgetsRepository.Verify(m => m.GetAll(this.userId), Times.Once, "The budgets should have been requested from the repository.");
                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.stubBudgets, okResult.Value, "The result from the repository should have been returned.");
            }
コード例 #3
0
            public void HandlesNotFound()
            {
                // Arrange.
                this.mockBudgetsRepository
                .Setup(m => m.Get(It.IsAny <long>(), It.IsAny <long>()))
                .Returns <Budget>(null);

                // Act.
                BudgetsController controller     = new BudgetsController(this.mockBudgetsRepository.Object);
                IActionResult     result         = controller.Get(this.id);
                NotFoundResult    notFoundResult = result as NotFoundResult;

                // Assert.
                Assert.IsNotNull(notFoundResult, "An object result should have been returned.");
                Assert.AreEqual(404, notFoundResult.StatusCode, "The status code from the response should have been 404.");
            }
コード例 #4
0
            public void HandlesGeneralException()
            {
                // Arrange.
                this.mockBudgetsRepository
                .Setup(m => m.Get(It.IsAny <long>(), It.IsAny <long>()))
                .Throws(new Exception());

                // Act.
                BudgetsController controller   = new BudgetsController(this.mockBudgetsRepository.Object);
                IActionResult     result       = controller.Get(this.id);
                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 getting the budget.";

                Assert.AreEqual(expectedMessage, objectResult.Value, "The error message should have been the result.");
            }
コード例 #5
0
            public void ReturnsNotFoundWhenRepositoryReturnsNull()
            {
                // Arrange.
                this.stubBudget = null;
                this.mockBudgetsRepository
                .Setup(m => m.Get(It.IsAny <long>(), It.IsAny <long>()))
                .Returns(this.stubBudget);

                // Act.
                BudgetsController controller     = new BudgetsController(this.mockBudgetsRepository.Object);
                IActionResult     result         = controller.Get(this.id);
                NotFoundResult    notFoundResult = result as NotFoundResult;

                // Assert.
                this.mockBudgetsRepository.Verify(m => m.Get(this.id, this.userId), Times.Once, "The budget should have been requested from the repository.");
                this.mockBudgetsRepository.VerifyNoOtherCalls();

                Assert.IsNotNull(notFoundResult, "A Not Found response should have been returned.");
                Assert.AreEqual(404, notFoundResult.StatusCode, "The status code from the response should have been 404.");
            }
コード例 #6
0
            public void ReturnsAnEmptyListWhenRepositoryReturnsNull()
            {
                // Arrange.
                this.stubBudgets = null;
                this.mockBudgetsRepository
                .Setup(m => m.GetAll(It.IsAny <long>()))
                .Returns(this.stubBudgets);

                // Act.
                BudgetsController controller = new BudgetsController(this.mockBudgetsRepository.Object);
                IActionResult     result     = controller.Get();
                OkObjectResult    okResult   = result as OkObjectResult;

                // Assert.
                this.mockBudgetsRepository.Verify(m => m.GetAll(this.userId), Times.Once, "The budgets should have been requested from the repository.");
                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(0, (okResult.Value as List <Budget>).Count, "An empty list should be the result.");
            }