模拟SignInManager的正确方法是什么?

时间:2018-04-02 20:19:43

标签: mocking asp.net-identity asp.net-core-webapi

使用DotNet Core 2.0.2编写WebAPI,如何测试依赖于SignInManager的控制器?我找到了一些描述如何模拟UserManager的地方,但未能揭示SignInManager的所有秘密。任何指针都将非常感激。

这是我正在尝试进行单元测试的课程:

namespace API.Controllers
{
    /// <summary>
    /// Represents an API endpoint that handles requests for client registration and authentication.
    /// </summary>
    [Route("api/[controller]")]
    public class AccountController : Controller, IAuthenticate, IRegister
    {
        private readonly SignInManager<ApplicationUser> signInManager;

        /// <summary>
        /// Constructs a new instance of the AccountController class.
        /// </summary>
        /// <param name="signInManager">Service that manages sign in operations for clients.</param>
        public AccountController(SignInManager<ApplicationUser> signInManager)
        {
            this.signInManager = signInManager;
        }

        /// <summary>
        /// Authenticates the specified credentials and returns a "200 OK" and a JWT with the client's authorization claims.
        /// If the specified credentials are invalid, returns a "401 Unauthorized" HTTP response.
        /// </summary>
        /// <param name="credentials">Identifying properties of the client requesting access to restricted content.</param>
        public async Task<IActionResult> Login(Credentials credentials)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Registers the client specified by the provided registration.
        /// </summary>
        /// <remarks>
        /// If the registration is successful, this method also logins the client.
        /// </remarks>
        /// <param name="registration">Identifying properties of the client requesting to have an account created.</param>
        public IActionResult Register(Registration registration)
        {
            throw new NotImplementedException();
        }
    }
}

我希望能够模拟SignInManager以验证它是否正确使用,例如:

mockSignInManager.Verify(m => m.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), false, false), Times.Once);

0 个答案:

没有答案