public async Task CreatePost_NotValidModel_ReturnsBadRequest() { blogController.ModelState.AddModelError("", ""); ActionResult result = await blogController.CreatePost(new Website.Models.DTOs.PostNewRequestDto()); Assert.IsInstanceOf <BadRequestObjectResult>(result); Assert.IsNotNull((result as BadRequestObjectResult).Value); }
public void TestCreatePost() { // Arrange var dbContext = DbContextMocker.GetDbContext(nameof(TestCreatePost)); var controller = new BlogController(dbContext); // Act var postModel = new PostModel() { Name = "Post 2", PostCategories = new List <CategoryModel>() { new CategoryModel() { Id = new Guid("A7C7E5BB-7805-4C80-A8E2-F2D3FD941FA0") // Category 1 Id from the DbContextMocker } } }; if (controller.CreatePost(postModel) is ObjectResult response) { // Assert that blog posts are now 2 Assert.True(response.Value is IList <PostModel> value && value.Count == 2); } dbContext.Dispose(); }
public async Task ViewShouldReturnCorrectViewModel() { var currentUser = new ApplicationUser { UserName = "******" }; var mockService = new Mock <IBlogService>(); mockService.Setup(x => x.IsInBlogRole(currentUser)) .ReturnsAsync(true); mockService.Setup(x => x.IsBlocked(currentUser)) .Returns(false); mockService.Setup(x => x.ExtractAllCategoryNames()) .ReturnsAsync(new List <string> { "Test1", "Test2", }); mockService.Setup(x => x.ExtractAllTagNames()) .ReturnsAsync(new List <string> { "Tag1", "Tag2", }); var mockUserManager = new Mock <UserManager <ApplicationUser> >( new Mock <IUserStore <ApplicationUser> >().Object, new Mock <IOptions <IdentityOptions> >().Object, new Mock <IPasswordHasher <ApplicationUser> >().Object, new IUserValidator <ApplicationUser> [0], new IPasswordValidator <ApplicationUser> [0], new Mock <ILookupNormalizer>().Object, new Mock <IdentityErrorDescriber>().Object, new Mock <IServiceProvider>().Object, new Mock <ILogger <UserManager <ApplicationUser> > >().Object); mockUserManager.Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .ReturnsAsync(currentUser); var httpContext = new DefaultHttpContext(); var tempData = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>()); var controller = new BlogController(mockService.Object, mockUserManager.Object) { TempData = tempData, }; var result = await controller.CreatePost(); Assert.IsType <ViewResult>(result); var viewResult = result as ViewResult; Assert.IsType <CreatePostIndexModel>(viewResult.Model); var viewModel = viewResult.Model as CreatePostIndexModel; Assert.Equal(2, viewModel.Categories.Count); Assert.Equal(2, viewModel.Tags.Count); }
public void CreatePost() { // Arrange Mock <IBlogRepository <Post> > mockRepo = new Mock <IBlogRepository <Post> >(); BlogController controller = new BlogController(mockRepo.Object); // Act ViewResult result = controller.CreatePost() as ViewResult; //Assert Assert.IsNotNull(result); }
public async Task ViewShouldRedirectCorrectForNoInRoleUser() { var currentUser = new ApplicationUser { UserName = "******" }; var mockService = new Mock <IBlogService>(); mockService.Setup(x => x.IsInBlogRole(currentUser)) .ReturnsAsync(false); var mockUserManager = new Mock <UserManager <ApplicationUser> >( new Mock <IUserStore <ApplicationUser> >().Object, new Mock <IOptions <IdentityOptions> >().Object, new Mock <IPasswordHasher <ApplicationUser> >().Object, new IUserValidator <ApplicationUser> [0], new IPasswordValidator <ApplicationUser> [0], new Mock <ILookupNormalizer>().Object, new Mock <IdentityErrorDescriber>().Object, new Mock <IServiceProvider>().Object, new Mock <ILogger <UserManager <ApplicationUser> > >().Object); mockUserManager.Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .ReturnsAsync(currentUser); var httpContext = new DefaultHttpContext(); var tempData = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>()); var controller = new BlogController(mockService.Object, mockUserManager.Object) { TempData = tempData, }; var result = await controller.CreatePost(); Assert.IsType <RedirectToActionResult>(result); var redirect = result as RedirectToActionResult; Assert.Equal($"Index", redirect.ActionName); Assert.Equal($"Blog", redirect.ControllerName); Assert.True(controller.TempData.ContainsKey("Error")); Assert.Equal(string.Format(ErrorMessages.NotInBlogRoles, Roles.Contributor), controller.TempData["Error"]); }