コード例 #1
0
        public void IndexReturnsAViewResultWithAListOfBrainstormSessions()
        {
            var mockRepo = new Mock<IBrainstormSessionRepository>();
            mockRepo.Setup(r => r.List()).Returns(GetTestSessions());
            var controller = new HomeController(mockRepo.Object);

            var result = Assert.IsType<ViewResult>(controller.Index());
            var model = Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>>
                (result.ViewData.Model);
            Assert.Equal(2, model.Count());
        }
コード例 #2
0
        public void IndexPostReturnsAViewResultWhenModelStateIsInvalid()
        {
            var mockRepo = new Mock<IBrainstormSessionRepository>();
            mockRepo.Setup(r => r.List()).Returns(GetTestSessions());
            var controller = new HomeController(mockRepo.Object);
            controller.ModelState.AddModelError("SessionName", "Required");
            var newSession = new HomeController.NewSessionModel();

            var result = Assert.IsType<ViewResult>(controller.Index(newSession));
            Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>>
                (result.ViewData.Model);
        }
コード例 #3
0
        public void IndexPostReturnsARedirectAndAddsSessionWhenModelStateIsValid()
        {
            var mockRepo = new Mock<IBrainstormSessionRepository>();
            mockRepo.Setup(r => r.Add(It.IsAny<BrainstormSession>())).Verifiable();
            var controller = new HomeController(mockRepo.Object);
            var newSession = new HomeController.NewSessionModel()
            { SessionName = "Test Name" };

            var result = Assert.IsType<RedirectToActionResult>
                (controller.Index(newSession));
            Assert.Equal("Home", result.ControllerName);
            Assert.Equal("Index", result.ActionName);
            mockRepo.Verify();
        }
コード例 #4
0
ファイル: HomeControllerTests.cs プロジェクト: CIPop/Docs
        public async Task IndexPost_ReturnsBadRequestResult_WhenModelStateIsInvalid()
        {
            // Arrange
            var mockRepo = new Mock<IBrainstormSessionRepository>();
            mockRepo.Setup(repo => repo.ListAsync()).Returns(Task.FromResult(GetTestSessions()));
            var controller = new HomeController(mockRepo.Object);
            controller.ModelState.AddModelError("SessionName", "Required");
            var newSession = new HomeController.NewSessionModel();

            // Act
            var result = await controller.Index(newSession);

            // Assert
            var badRequestResult = Assert.IsType<BadRequestObjectResult>(result);
            Assert.IsType<SerializableError>(badRequestResult.Value);
        }
コード例 #5
0
ファイル: HomeControllerTests.cs プロジェクト: CIPop/Docs
        public async Task Index_ReturnsAViewResult_WithAListOfBrainstormSessions()
        {
            // Arrange
            var mockRepo = new Mock<IBrainstormSessionRepository>();
            mockRepo.Setup(repo => repo.ListAsync()).Returns(Task.FromResult(GetTestSessions()));
            var controller = new HomeController(mockRepo.Object);

            // Act
            var result = await controller.Index();

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            var model = Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>>(
                viewResult.ViewData.Model);
            Assert.Equal(2, model.Count());
        }
コード例 #6
0
ファイル: HomeControllerTests.cs プロジェクト: CIPop/Docs
        public async Task IndexPost_ReturnsARedirectAndAddsSession_WhenModelStateIsValid()
        {
            // Arrange
            var mockRepo = new Mock<IBrainstormSessionRepository>();
            mockRepo.Setup(repo => repo.AddAsync(It.IsAny<BrainstormSession>()))
                .Returns(Task.CompletedTask)
                .Verifiable();
            var controller = new HomeController(mockRepo.Object);
            var newSession = new HomeController.NewSessionModel()
            {
                SessionName = "Test Name"
            };

            // Act
            var result = await controller.Index(newSession);

            // Assert
            var redirectToActionResult = Assert.IsType<RedirectToActionResult>(result);
            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
            mockRepo.Verify();
        }