我有一个控制器,我在那里调用userId:
var userId = User.Identity.GetUserId();
这使我的单元测试失败,因为User为null。 我尝试使用此方法在测试方法中设置用户,但在测试运行时,用户在控制器中仍为空。
var context = new Mock<HttpContextBase>();
var mockIdentity = new Mock<IIdentity>();
context.SetupGet(x => x.User.Identity).Returns(mockIdentity.Object);
mockIdentity.Setup(u => u.Name).Returns("test_userName");
关于我做错了什么的暗示?
答案 0 :(得分:2)
HttpContextBase.User
的类型为IPrincipal
,您尚未嘲笑它。这就是为什么在测试中访问它时它返回null的原因。你可以按照以下方式嘲笑它
var controllerContext = new Mock<ControllerContext>();
var principal = new Moq.Mock<IPrincipal>();
principal.Setup(p => p.IsInRole("Administrator")).Returns(true);
principal.SetupGet(x => x.Identity.Name).Returns(userName);
controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
controller.ControllerContext = controllerContext.Object;
以下是此代码的reference
答案 1 :(得分:0)
好的,所以根据this帖子,您可以这样做:
public class MyController: Controller
{
public Func<string> GetUserId; //For testing
public MyController()
{
GetUserId = () => User.Identity.GetUserId();
}
//controller actions
}
然后,当您需要用户时,而不是在调用User.Identity.GetUserId()
,而只需调用GetUserId()
并模拟测试中的用户ID:
controller = new MyController()
{
GetUserId = () => "IdOfYourChoosing"
};
答案 2 :(得分:0)
您正在模拟HttpContextBase但未将其传递给您的控制器。 试试这个模式:
类别:
public MyClass
{
private readonly HttpContextBase _contextBase;
public MyClass(HttpContextBase contextBase)
{
this._contextBase = contextBase;
}
public void Process()
{
var userId = _contextBase.User.Identity;
}
}
测试:
[Test]
public void MyClass_Test_SO()
{
// arrange
var context = new Mock<HttpContextBase>();
var mockIdentity = new Mock<IIdentity>();
context.SetupGet(x => x.User.Identity).Returns(mockIdentity.Object);
mockIdentity.Setup(u => u.Name).Returns("test_userName");
var sut = new MyClass(context.Object);
// act
sut.Process();
// assert
// ... whatever
}