c# - How to access HttpContextBase in MSTEST - Stack Overflow

I see answer after answer to how to get HttpContext, and the first thing it requires is access to HttpC

I see answer after answer to how to get HttpContext, and the first thing it requires is access to HttpContextBase. However, I do not have access to HttpContextBase in the MSTest context ( v8, Test SDK 17.13.0, and MSTest 3.8.3).

I've tried too many things to list (or even remember). I've tried to find the AspCore and adding it to dependencies, but no luck. I've also tried a variety of different project types, but the only ones that have MSTest enabled seem to not have Asp enabled.

I'm obviously missing something, does anyone have any idea what?

I see answer after answer to how to get HttpContext, and the first thing it requires is access to HttpContextBase. However, I do not have access to HttpContextBase in the MSTest context ( v8, Test SDK 17.13.0, and MSTest 3.8.3).

I've tried too many things to list (or even remember). I've tried to find the AspCore and adding it to dependencies, but no luck. I've also tried a variety of different project types, but the only ones that have MSTest enabled seem to not have Asp enabled.

I'm obviously missing something, does anyone have any idea what?

Share Improve this question edited Mar 26 at 2:51 Qiang Fu 9,3871 gold badge6 silver badges16 bronze badges asked Mar 25 at 20:34 Clifford AndersonClifford Anderson 1 1
  • Show us a screenshot of your project dependencies for your test project. – mjwills Commented Mar 25 at 23:48
Add a comment  | 

1 Answer 1

Reset to default 0

HttpContextBase is available in old .NET framework but no in 8. You could reference following sample to mock HttpContext.
UserController.cs (To be test)

    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        [HttpGet("username")]
        public IActionResult GetUserName()
        {
            var userName = HttpContext.User?.Identity?.Name ?? "Anonymous";
            return Ok(userName);
        }
    }

Test with mock HttpContext

    [TestClass]
    public class UserControllerTests
    {
        private Mock<HttpContext> _mockHttpContext;
        private UserController _controller;

        [TestInitialize]
        public void Setup()
        {
            _mockHttpContext = new Mock<HttpContext>();
            _controller = new UserController();
        }

        [TestMethod]
        public void GetUserName_ShouldReturnTestUser()
        {
            // Arrange
            var claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "TestUser") });
            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

            _mockHttpContext.Setup(ctx => ctx.User).Returns(claimsPrincipal);

            // Assign the mock HttpContext to the controller
            _controller.ControllerContext = new ControllerContext
            {
                HttpContext = _mockHttpContext.Object
            };

            // Act
            var result = _controller.GetUserName() as OkObjectResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(200, result.StatusCode);
            Assert.AreEqual("TestUser", result.Value);
        }

        [TestMethod]
        public void GetUserName_ShouldReturnAnonymous_WhenNoUser()
        {
            // Arrange
            _mockHttpContext.Setup(ctx => ctx.User).Returns(new ClaimsPrincipal());

            // Assign the mock HttpContext to the controller
            _controller.ControllerContext = new ControllerContext
            {
                HttpContext = _mockHttpContext.Object
            };

            // Act
            var result = _controller.GetUserName() as OkObjectResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(200, result.StatusCode);
            Assert.AreEqual("Anonymous", result.Value);
        }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744169785a4561480.html

相关推荐

  • c# - How to access HttpContextBase in MSTEST - Stack Overflow

    I see answer after answer to how to get HttpContext, and the first thing it requires is access to HttpC

    9天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信