如何测试,需要用户登录方法 [英] How can I test methods which needs user to be logged

查看:131
本文介绍了如何测试,需要用户登录方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了一些code,需要先登录的用户。当我试图用的AccountController 来登录,它看起来一切正常,但的AccountController (的IPrincipal)用户仍是空。我怎样才能正确登录(或更好,我可以嘲笑它在某种程度上)?

 公共异步任务SetupAsync()
        {
            VAR语境=新的DataContext();
            VAR经理=新的UserManager(新UserStore(上下文));
            VAR的AccountController =新的AccountController(经理);
            VAR mockAuthenticationManager =新的模拟< IAuthenticationManager>();
            mockAuthenticationManager.Setup(上午= GT; am.SignOut());
            mockAuthenticationManager.Setup(上午= GT; am.SignIn());
            accountController.AuthenticationManager = mockAuthenticationManager.Object;
            VAR用户=新LoginViewModel
            {
                电子邮件=user@wp.pl
                密码=USERUSER
                与rememberMe = FALSE
            };
            如果(manager.FindByEmail(user@wp.pl)== NULL)
            {
                等待manager.CreateAsync(新用户{EMAIL =user@wp.pl,用户名=user@wp.pl},USERUSER);
            }
            等待accountController.Login(用户,家/指数);
            _calendarController =新CalendarController(背景);
        }

在这里,我得到了用户的空异常:

 公共ClaimsPrincipal的currentUser
        {
            {返回新ClaimsPrincipal((System.Security.Claims.ClaimsPrincipal)this.User); }
        }

编辑:在回行,我还有用户属性为null。这是由样本的AccountController

  VAR用户=等待_userManager.FindAsync(model.Email,model.Password);            如果(用户!= NULL)
            {
                等待SignInAsync(用户,model.RememberMe);
                返回RedirectToAction(指数,日历);
            }


解决方案

想通了我自己,可能不是完美的解决方案,但我很高兴呢。 @andreasnico你的答案帮助,谢谢。

我在嘲笑我的自定义ClaimsPrincipal,并设置用户ID - 这就是我真正需要的。

  VAR mockCp =新的模拟< IClaimsPrincipal>();
mockCp.SetupGet(CP => cp.UserId).Returns(user.Id);
_calendarController.CurrentUser = mockCp.Object;

I'm testing some code which needs user to be logged in. When I'm trying to log in with AccountController, it's looks like everything is working, but at AccountController (IPrincipal) User is still null. How can I properly log in (or better, can I mock it somehow)?

public async Task SetupAsync()
        {
            var context = new DataContext();
            var manager = new UserManager(new UserStore(context));
            var accountController = new AccountController(manager);
            var mockAuthenticationManager = new Mock<IAuthenticationManager>();
            mockAuthenticationManager.Setup(am => am.SignOut());
            mockAuthenticationManager.Setup(am => am.SignIn());
            accountController.AuthenticationManager = mockAuthenticationManager.Object;
            var user = new LoginViewModel
            {
                Email = "user@wp.pl",
                Password = "useruser",
                RememberMe = false
            };
            if (manager.FindByEmail("user@wp.pl") == null)
            {
                await manager.CreateAsync(new User { Email = "user@wp.pl", UserName = "user@wp.pl" }, "useruser");
            }
            await accountController.Login(user, "home/index");
            _calendarController = new CalendarController(context);
        }

Here I got User null exception:

public ClaimsPrincipal CurrentUser
        {
            get { return new ClaimsPrincipal((System.Security.Claims.ClaimsPrincipal)this.User); }
        }

Edit: At return line, I have still User property null. This is sample from AccountController:

var user = await _userManager.FindAsync(model.Email, model.Password);

            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToAction("index", "calendar");
            }

解决方案

Figured it out on my own, probably not elegant solution but I'm happy anyway. @andreasnico your answer helped, thanks.

I'm mocking my custom ClaimsPrincipal, and setting up UserId - that's what I really needed.

var mockCp = new Mock<IClaimsPrincipal>();
mockCp.SetupGet(cp => cp.UserId).Returns(user.Id);
_calendarController.CurrentUser = mockCp.Object;

这篇关于如何测试,需要用户登录方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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