public void TestGetArticleById() { var contextMock = new Mock <HttpContext>(); var claims = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "abc"), new Claim(ClaimTypes.Role, "member"), new Claim("user_id", "5d247a04eff1030d7c520a222") }); contextMock.Setup(x => x.User).Returns(new ClaimsPrincipal(claims)); mockArticleService.Setup(x => x.GetArticleById(It.IsAny <string>(), It.IsAny <string>())).Returns(article); var _articleController = new ArticleController(mockArticleService.Object, mockPostService.Object); _articleController.ControllerContext.HttpContext = contextMock.Object; var getArticleById = _articleController.GetArticleById("5d247a04eff1030d7c520a1212"); var type = getArticleById.GetType(); Assert.AreEqual(type.Name, "OkObjectResult"); }
public void TestGetArticleByIdIsAuthenticated() { var contextMock = new Mock <HttpContext>(); var claims = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "abc"), new Claim(ClaimTypes.Role, "member"), new Claim("user_id", "5d247a04eff1030d7c520a7uu77") }, "authenticationType"); contextMock.Setup(x => x.User).Returns(new ClaimsPrincipal(claims)); mockArticleService.Setup(x => x.GetArticleById(It.IsAny <string>(), It.IsAny <string>())).Returns(article); var _articleController = new ArticleController(mockArticleService.Object, mockPostService.Object); _articleController.ControllerContext.HttpContext = contextMock.Object; IActionResult getArticleById = _articleController.GetArticleById("5d247a04eff1030d7c520ac123"); var okObjectResult = getArticleById as OkObjectResult; Article articleActual = (Article)okObjectResult.Value; Assert.AreEqual(articleActual, article); }