使用UserManager的ASP.NET 5单元测试控制器

Kev*_*rim 5 c# asp.net unit-testing moq

我开始使用ASP.NET 5(vNext),现在我正在尝试对使用UserManager注册和登录用户的控制器进行单元测试.

我正在使用xUnitmoq.netcore.

在我的UnitTest中,我有:

var mockStore = new Mock<IUserStore<ApplicationUser>>(MockBehavior.Strict).As<IUserPasswordStore<ApplicationUser>>();

mockStore.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>(), CancellationToken.None)).Returns(Task.FromResult(new IdentityResult()));

var userManager = new UserManager<ApplicationUser>(mockStore.Object, null, null, null, null, null, null, null, null, null);
Run Code Online (Sandbox Code Playgroud)

和控制器中的代码:

var result = await _securityManager.CreateAsync(user, model.Password);
Run Code Online (Sandbox Code Playgroud)

但是,当我运行单元测试时,我在调用CreateAsync时得到一个null异常.

我不明白为什么我不能用ApplicationUser和字符串参数模拟CreateAsync方法,我必须使用CancellationToken来模拟它.

这是我的第一个使用ASP.NET 5的项目,之前我使用的是ASP.NET 4.6,所以现在很多东西都不同了.

并且,您认为我应该已经在ASP.NET 5中开发了一个重要的项目,还是应该等待并为ASP.NET 4.6开发它?

Kev*_*rim 9

在尝试了很多东西后,我想出了一个有效的解决方案.

的UserManager

我创建了一个继承UserManager并覆盖CreateAsync方法的FakeUserManager类.

public class FakeUserManager : UserManager<ApplicationUser>
{
    public FakeUserManager()
        : base(new Mock<IUserStore<ApplicationUser>>().Object,
              new Mock<IOptions<IdentityOptions>>().Object,
              new Mock<IPasswordHasher<ApplicationUser>>().Object,
              new IUserValidator<ApplicationUser>[0],
              new IPasswordValidator<ApplicationUser>[0],
              new Mock<ILookupNormalizer>().Object,
              new Mock<IdentityErrorDescriber>().Object,
              new Mock<IServiceProvider>().Object,
              new Mock<ILogger<UserManager<ApplicationUser>>>().Object,
              new Mock<IHttpContextAccessor>().Object)
    { }

    public override Task<IdentityResult> CreateAsync(ApplicationUser user, string password)
    {
        return Task.FromResult(IdentityResult.Success);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我将一个新的FakeUserManager实例传递给AccountController构造函数,它工作正常.


SignInManager

对于那些可能需要模拟SignInManager的人,我按照以下方式做到了:

我创建了一个FakeSignInManager类,它继承了SignInManager并覆盖了我需要的方法.

public class FakeSignInManager : SignInManager<ApplicationUser>
{
    public FakeSignInManager(IHttpContextAccessor contextAccessor)
        : base(new FakeUserManager(),
              contextAccessor,
              new Mock<IUserClaimsPrincipalFactory<ApplicationUser>>().Object,
              new Mock<IOptions<IdentityOptions>>().Object,
              new Mock<ILogger<SignInManager<ApplicationUser>>>().Object)
    {
    }

    public override Task SignInAsync(ApplicationUser user, bool isPersistent, string authenticationMethod = null)
    {
        return Task.FromResult(0);
    }

    public override Task<SignInResult> PasswordSignInAsync(string userName, string password, bool isPersistent, bool lockoutOnFailure)
    {
        return Task.FromResult(SignInResult.Success);
    }

    public override Task SignOutAsync()
    {
        return Task.FromResult(0);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于SignInManager需要一个上下文访问器来工作,我使FakeSignInManager在构造函数中接收它.

然后,在创建SignInManager的新实例之前,我按以下方式准备一个新的HttpContextAccessor:

var context = new Mock<HttpContext>();
var contextAccessor = new Mock<IHttpContextAccessor>();
contextAccessor.Setup(x => x.HttpContext).Returns(context.Object);
Run Code Online (Sandbox Code Playgroud)

然后创建新的FakeSignInManager实例:

new FakeSignInManager(contextAccessor.Object);
Run Code Online (Sandbox Code Playgroud)