public void GivenALoggedInUser_WhenCreateIsCalled_AndTheModelIsInvalid_ThenTheNewViewIsReturned() { MockHttpContext.SetupProperty(h => h.User); SetControllerContext(_controller); _controller.HttpContext.User = new UserViewModel { IsLoggedIn = true }; _controller.ModelState.AddModelError("error", "message"); var result = _controller.Create(new CreateBlogViewModel()) as ViewResult; Assert.That(result, Is.Not.Null); Assert.That(result.ViewName, Is.EqualTo("New").IgnoreCase); }
public async Task CreateTest_Post_ReturnsCreateView_WhenModelStateIsInvalid() { // Arrange var res = false; var objectsBlogPost = new BlogPost { Title = "mock article 1", Description = "Hi", PostedDate = DateTime.Now }; blogController.ModelState.AddModelError("Description", "This field is required"); // Act var result = await blogController.Create(objectsBlogPost); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Equal(objectsBlogPost, viewResult.ViewData.Model); blogPostServMock.Verify(repo => repo.Create(objectsBlogPost), Times.Never()); if (viewResult == Assert.IsType <ViewResult>(result)) { res = true; } File.AppendAllText("../../../../output_exception_revised.txt", "CreateTest_Post_ReturnsCreateView_WhenModelStateIsInvalid=" + res.ToString() + "\n"); }
public void Create_Ok() { //Act var response = _blogController.Create(); //Assert Assert.True(response.CastActionResult <ViewResult>() != null); }
public void Create_Non_AuthorizedUser_ShouldShowLoginView() { var mockRepo = new Mock <IBlogRepository>(); var controller = new BlogController(mockRepo.Object) { ControllerContext = MockHelpers.FakeControllerContext(false) }; var result = controller.Create() as ViewResult; Assert.IsNotNull(result); Assert.IsNull(result.ViewName); }
public void CreateAddsBlog() { //Arrange Blog blog = new Blog { BlogTitel = "Third test-blog", Closed = false }; var controller = new BlogController(_repository.Object, _manager); //Act ActionResult result = controller.Create(blog); //Assert _repository.Verify(b => b.AddBlog(blog)); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void CreateDoesNotAddBlog() { //Arrange Blog blog = new Blog { BlogTitel = "Third test-blog", Closed = false }; var controller = new BlogController(_repository.Object, _manager); controller.ModelState.AddModelError("error", "error"); //Act ActionResult result = controller.Create(blog); //Assert _repository.Verify(b => b.AddBlog(It.IsAny <Blog>()), Times.Never()); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Test3_Create() { // Arrange var controller = new BlogController(_context); // Act var result = getListResult(controller.Create( new CreateAndUpdateBody { title = "my title", slug = "my slug", text = "my text" })); // Assert Assert.Equal("my title", result[1].Title); Assert.Equal("my-slug", result[1].Slug); Assert.Equal("my text", result[1].Text); Assert.False(result[1].IsDeleted); }
public async Task CreateRedirectActionRedirectsToIndexActionAsync() { var controller = new BlogController(_repository.Object) { ControllerContext = MockHelpers.FakeControllerContext(false) }; var tempData = new TempDataDictionary(controller.ControllerContext.HttpContext, Mock.Of <ITempDataProvider>()); controller.TempData = tempData; var blog = new Blog() { Name = "My blog", Description = "Description of blog" }; var result = Task.FromResult(await controller.Create(blog) as RedirectToActionResult); Assert.IsNotNull(result, "RedirectToIndex needs to redirect to the Index action"); Assert.AreEqual("Index", result.Result.ActionName); }
public async Task CreateTest_Post_AddsBlogPostToRepository_AndRedirectsToIndex() { var res = false; // Arrange var objectsBlogPost = new BlogPost { Title = "mock article 1", Description = "Hi", PostedDate = DateTime.Now }; // Act var result = await blogController.Create(objectsBlogPost); // Assert blogPostServMock.Verify(repo => repo.Create(objectsBlogPost)); var viewResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", viewResult.ActionName); if (viewResult.ActionName == "Index") { res = true; } File.AppendAllText("../../../../output_revised.txt", "CreateTest_Post_AddsBlogPostToRepository_AndRedirectsToIndex=" + res.ToString() + "\n"); }
public async Task CreateViewIsReturnedWhenInputIsNotValidAsync() { var blog = new Blog() { Name = "", Description = "", }; var controller = new BlogController(_repository.Object); var validationContext = new ValidationContext(blog, null, null); var validationResults = new List <ValidationResult>(); Validator.TryValidateObject(blog, validationContext, validationResults, true); foreach (var validationResult in validationResults) { controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage); } var result = await controller.Create(blog); Assert.IsNotNull(Task.FromResult(result)); Assert.IsTrue(validationResults.Count > 0); }