在以前的版本中,您可以User
直接在控制器上进行设置,这使一些非常简单的单元测试成为可能。
如果您查看ControllerBase的源代码,您会注意到User
提取自HttpContext
。
public ClaimsPrincipal User => HttpContext?.User;
控制器访问HttpContext
通孔ControllerContext
public HttpContext HttpContext => ControllerContext.HttpContext;
您会注意到这两个是只读属性。好消息是,ControllerContext
属性允许设置其值,这将是您的理想选择。
所以目标是到达那个物体。由于CoreHttpContext
是抽象的,因此易于模拟。
假设控制器像
public class MyController : Controller {
IMyContext _context;
public MyController(IMyContext context) {
_context = context;
}
public IActionResult Index() {
SettingsViewModel svm = _context.MySettings(User.Identity.Name);
return View(svm);
}
}
使用Moq,测试看起来可能像这样
public void Given_User_Index_Should_Return_ViewResult_With_Model() {
var username = "FakeUserName";
var identity = new GenericIdentity(username, "");
var mockPrincipal = new Mock<ClaimsPrincipal>();
mockPrincipal.Setup(x => x.Identity).Returns(identity);
mockPrincipal.Setup(x => x.IsInRole(It.IsAny<string>())).Returns(true);
var mockHttpContext = new Mock<HttpContext>();
mockHttpContext.Setup(m => m.User).Returns(mockPrincipal.Object);
var model = new SettingsViewModel() {
};
var mockContext = new Mock<IMyContext>();
mockContext.Setup(m => m.MySettings(username)).Returns(model);
var controller = new MyController(mockContext.Object) {
ControllerContext = new ControllerContext {
HttpContext = mockHttpContext.Object
}
};
var viewResult = controller.Index() as ViewResult;
Assert.IsNotNull(viewResult);
Assert.IsNotNull(viewResult.Model);
Assert.AreEqual(model, viewResult.Model);
}
new Claim(ClaimTypes.Name, "1")
为了匹配控制器的使用user.Identity.Name
;否则,这正是我要实现的目标……Danke schon!