在ClaimsPrincipal类上模拟扩展方法以在HttpContext.User

问题描述

Moq等框架无法扩展,因为它们是静态方法

我的ClaimsPrincipal类具有一个扩展方法,该扩展方法在我的应用程序中得到了广泛使用,并且无法更改。扩展方法称为GetUserSerial()

我希望能够以某种方式模拟此扩展方法,以便可以在单元测试中使用它,因为该扩展方法本身已在各种ASP.NET Core控制器中使用,因此我需要能够模拟输出以便正确测试控制器。

我正在尝试通过以下操作来模拟ControllerContext

private Mock<ControllerContext> GetMockControllerContext(Mock<ClaimsPrincipal> mockClaimsPrincipal)
        {
            if (mockClaimsPrincipal == null)
            {
                mockClaimsPrincipal = new Mock<ClaimsPrincipal>();
                mockClaimsPrincipal.Setup(x => x.GetSerialNo()).Returns("serial");
            }

            var contextMock = new Mock<HttpContext>();
            contextMock.SetupGet(ctx => ctx.User).Returns(mockClaimsPrincipal.Object);

            var controllerContextMock = new Mock<ControllerContext>();
            controllerContextMock.SetupGet(con => con.HttpContext).Returns(contextMock.Object);
            return controllerContextMock;
        }
public static string GetSerialNo(this ClaimsPrincipal claimsPrincipal)
        {
            return claimsPrincipal?.Claims?.FirstOrDefault(c => c.Type == "SerialNo")?.Value;
        }

但是,这显然会产生运行时错误,告诉我无法模拟扩展方法

这里的主要问题是扩展方法正在控制器本身内部使用,我无法更改其输出

有没有办法解决这个问题,以便我可以规定GetSerialNo方法输出并将其提供给我的ControllerContext

解决方法

无需模拟。创建具有所需声明的主体实例

string expectedSerial = "serial";
var claims = new List<Claim> {
    new Claim("SerialNo",expectedSerial),};
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims));

//...

这样,当调用扩展时,它将表现出预期的效果。

如果修改现有身份,则只需添加声明

//... principal already exists

string expectedSerial = "serial";
Claim claim = new Claim("SerialNo",expectedSerial);
(principal.Identity as ClaimsIdentity).AddClaim(claim);

//...