示例#1
0
        public async Task ViewShouldRedirectCorrectForNoInRoleUser()
        {
            var currentUser = new ApplicationUser {
                UserName = "******"
            };

            var mockService = new Mock <IBlogService>();

            mockService.Setup(x => x.IsInPostRole(currentUser, Guid.NewGuid().ToString()))
            .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.DeletePost(Guid.NewGuid().ToString());

            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(ErrorMessages.NotInDeletePostRoles, controller.TempData["Error"]);
        }
示例#2
0
        public void TestDelete()
        {
            this.Setup();
            var context2 = CreateContext(nameDB);

            Post post = new Post
            {
                PostId   = 1,
                Title    = "Post1",
                Body     = "Body1",
                Date     = DateTime.Now.Date,
                AuthorId = 1,
                Author   = new Author {
                    Name = "Author1", Pass = "******", Mail = "*****@*****.**"
                }
            };

            var controller = new BlogController(context2);
            var result     = controller.DeletePost(1).Result;

            Assert.AreEqual(post, result.Value);
        }
示例#3
0
        public void DeletePost()
        {
            ActionResult result = blogController.DeletePost(0);

            Assert.IsInstanceOf <OkResult>(result);
        }