ASP.NET Core对Controller进行单元测试的完整步骤

2020-06-19 10:59:26王旭

  var ctrl = new AccountController();
  ctrl.ControllerContext = new ControllerContext();
  ctrl.ControllerContext.HttpContext = new DefaultHttpContext();

对HttpContext.SignInAsync进行mock

我们使用ASP.NET Core框架进行登录认证的时候,往往使用HttpContext.SignInAsync进行认证授权,所以单元测试的时候也需要进行mock。下面是一个典型的登录Action,对密码进行认证后调用SignInAsync在客户端生成登录凭证,否则跳到登录失败页面。

  public async Task<IActionResult> Login(string password)
    {
      if (password == "123")
      {
        var claims = new List<Claim>
        {
         new Claim("UserName","x")
        };
        var authProperties = new AuthenticationProperties
        {
        };
        var claimsIdentity = new ClaimsIdentity(
         claims, CookieAuthenticationDefaults.AuthenticationScheme);
        await HttpContext.SignInAsync(
          CookieAuthenticationDefaults.AuthenticationScheme,
          new ClaimsPrincipal(claimsIdentity),
          authProperties);
        return Redirect("login_success");
      }

      return Redirect("login_fail");
    }

HttpContext.SignInAsync其实个时扩展方法,SignInAsync其实最终是调用了IAuthenticationService里的SignInAsync方法。所以我们需要mock的就是IAuthenticationService接口,否者代码走到HttpContext.SignInAsync会提示找不到IAuthenticationService的service。而IAuthenticationService本身是通过IServiceProvider注入到程序里的,所以同时需要mock接口IServiceProvider。

  [TestMethod()]
    public async Task LoginTest()
    {
      var ctrl = new AccountController();

      var authenticationService = new Mock<IAuthenticationService>();
      var sp = new Mock<IServiceProvider>();
      sp.Setup(s => s.GetService(typeof(IAuthenticationService)))
        .Returns(() => {
          return authenticationService.Object;
        });
      ctrl.ControllerContext = new ControllerContext();
      ctrl.ControllerContext.HttpContext = new DefaultHttpContext();
      ctrl.ControllerContext.HttpContext.RequestServices = sp.Object;

      var result = await ctrl.Login("123");
      Assert.IsNotNull(result);
      Assert.IsInstanceOfType(result, typeof(RedirectResult));
      var rr = result as RedirectResult;
      Assert.AreEqual("login_success", rr.Url);

      result = await ctrl.Login("1");
      Assert.IsNotNull(result);
      Assert.IsInstanceOfType(result, typeof(RedirectResult));
      rr = result as RedirectResult;
      Assert.AreEqual("login_fail", rr.Url);
    }

对HttpContext.AuthenticateAsync进行mock

HttpContext.AuthenticateAsync同样比较常用。这个扩展方法同样是在IAuthenticationService里,所以测试代码跟上面的SignInAsync类似,只是需要对AuthenticateAsync继续mock返回值success or fail。

   public async Task<IActionResult> Login()
    {
      if ((await HttpContext.AuthenticateAsync()).Succeeded)
      {
        return Redirect("/home");
      }

      return Redirect("/login");
    }