ASP.NET MVC 5-仅使用身份的单元测试服务 [英] ASP.NET MVC 5 - Unit Testing Service that uses only Identity

查看:45
本文介绍了ASP.NET MVC 5-仅使用身份的单元测试服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类,实际上,它仅在ASP.NET Identity之上进行操作.

I have this class, that, actually, only does operations on top of ASP.NET Identity.

 public class AuthenticationService : IAuthenticationService
    {
        private readonly ApplicationSignInManager _signInManager;
        private readonly ApplicationUserManager _userManager;
        private readonly IAuthenticationManager _authManager;
        private readonly HttpContextBase _context;

        public AuthenticationService(
            ApplicationSignInManager signInManager, 
            ApplicationUserManager userManager, 
            IAuthenticationManager authManager,
            HttpContextBase context)
        {
            _signInManager = signInManager;
            _userManager = userManager;
            _authManager = authManager;
            _context = context;
        }

        public async Task<SignInStatus> SignIn(string email, string password)
        {
            var result = await _signInManager.PasswordSignInAsync(email, password, true, shouldLockout: false);
            return result;
        }

        public async Task<IdentityResult> Register(string email, string password)
        {
            var user = new AppUser
            {
                UserName = email,
                Email = email,
            };

            var result = await _userManager.CreateAsync(user, password);

            return result;
        }

        public void SignOut()
        {
            _authManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        }

        public async Task<IdentityResult> ForgotPassword(string email, string newPassword)
        {
            var user = await _userManager.FindByEmailAsync(email);
            string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
            var result = await _userManager.ResetPasswordAsync(user.Id, resetToken, newPassword);

            return result;
        }

        public async Task<AppUser> GetUserByEmail(string email)
        {
            var user = await _userManager.FindByEmailAsync(email);
            return user;
        }

        public string GetLoggedInUserId()
        {
            if(_context.User.Identity.IsAuthenticated)
            {
                return _context.User.Identity.GetUserId();
            }
            return string.Empty;
        }
    }

我不知道我是否要注入正确的依赖项.但是我的问题是:

I don't know if I'm injecting the right dependencies. But my question is:

在这样的课程中,进行单元测试有意义吗?由于大多数方法都会调用与身份相关的方法,并且只会返回结果.

似乎唯一正确的测试是,如果没有通过身份验证的用户尝试获取用户ID,将会发生什么情况.

The only test that it seems right to do is what happens when I try to get the ID of a user if no user is authenticated.

推荐答案

在几乎所有可以测试的类中,都假设它是对一个实体的测试以及所有内部依赖项(例如 ApplicationUserManager IAuthenticationManager ):

In almost any class you can test, assuming that it's a unit test of one entity under test and all internal dependencies(like ApplicationUserManager or IAuthenticationManager) are mocked:

  1. 所有内部服务被称为确切次数
  2. 被测实体不会更改方法中传递的参数,而是将其正确传递给内部服务的调用(例如 ApplicationUserManager IAuthenticationManager )

因此,例如,您可以在 SignIn 中测试,对于 email 密码-与传递给 SignIn 的密码相同;第三个未命名的参数和 shouldLockout (根据业务逻辑,它们是对和错).

So, for example, you can test in SignIn that _signInManager.PasswordSignInAsync gets called exactly once and arguments are, for email and password — the same as they were passed into SignIn; for the third unnamed argument and shouldLockout — they are according to business logic — true and false.

ForgotPassword 比较棘手:您已将数据从 _userManager.FindByEmailAsync 传递到 _userManager.GeneratePasswordResetTokenAsync ,然后传递给 _userManager.ResetPasswordAsync .
在这种情况下,您不需要测试这三个调用的结果是否正确,因为应该在测试 _userManager 的过程中测试这种情况,但是您将需要测试是否已对所有这些服务进行了调用确切的次数.

ForgotPassword is trickier: you have data passed from _userManager.FindByEmailAsync to _userManager.GeneratePasswordResetTokenAsync and then to _userManager.ResetPasswordAsync.
In this case, you don't need to test results of these three calls are correct as such thing should be tested in tests for _userManager, but you will need to test that calls to all these services are made exact number of times.

这篇关于ASP.NET MVC 5-仅使用身份的单元测试服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆