单元测试 - httpcontext为null,websecurity.CurrentUserId也未填充

时间:2013-06-13 21:00:58

标签: asp.net-mvc-4 mocking nunit security

我有一个MVC 4应用程序,我正在构建单元测试。在我的GameController中,我有一个Action,JoinGame,它需要当前的userid。我在控制器内部使用WebSecurity.CurrentUserId得到了这个。

当我为JoinGame运行单元测试时,没有填充UserId。显然,在单元测试期间,没有“当前”用户。我正在试图弄清楚如何嘲笑一个。

我得到的第一个错误是System.ArgumentNullException: Value cannot be null. Parameter name; httpContext

然后我找到了这个Mocking WebSecurity provider

我创建了一个包装器界面& websecurity的类,并嘲笑包装器&将websecuritywrapper注入游戏控制器。这解决了httpContext(虽然我现在并不真正理解为什么这样做而且HttpContextFactory没有),但是websecuritywrapper返回的CurrentUserId总是为0.即使我将它硬编码为1内部websecuritywrapper类({{1 }}

显然我做错了什么,只是不确定是什么。我已经为单元测试,控制器和下面的包装器发布了代码。

public int CurrentUserId{ get { return 1; }}

public RedirectToRouteResult JoinGame(int gameid)
    {
        //_wr is the websecuritywrapper
        int UserID =  _wr.CurrentUserId; //WebSecurity.CurrentUserId;            

        // get userteam for this user and this game
        UserTeam ut = _UserTeamRepository.GetUserTeam(userteamid:0, gameid: gameid, userid: UserID);
        int intUTID = 0;
        if (ut == null)
        {
            // no userteam found, create it
            OperationStatus opStatus = _UserTeamRepository.CreateUserTeam(UserID, gameid);
            if (opStatus.Status) intUTID = (int)opStatus.OperationID;
        }
        else {intUTID = ut.Id; }
        if (intUTID > 0)
        {
            return RedirectToAction("Index", "ViewPlayers", new { id = intUTID });
        }
        else
        {
            return RedirectToAction("Index", "Game");
        }           
    }

[Test]
    public void Game_JoinGame_Returns_RedirectToAction()
    {
        UserProfile creator = new UserProfile();
        UserProfile user = new UserProfile();
        Game game = new Game();
        ICollection<UserTeam> uteams = null;
        UserTeam ut = new UserTeam();
        ICollection<UserTeam_Player> utp = null;

        List<Game> games = new List<Game>
        {
            new Game { Id = 1, CreatorId = 1, Name = "Game1", Creator = creator, UserTeams=uteams},
        };

        List<UserTeam> userteams = new List<UserTeam>
        {
            new UserTeam {Id=1, UserId = 1, GameId=1, User=user, Game = game, UserTeam_Players=utp}
        };

        Mock<IGameRepository> mockGameRepository = new Mock<IGameRepository>();
        Mock<IUserTeamRepository> mockUserTeamRepository = new Mock<IUserTeamRepository>();
        Mock<IWebSecurityWrapper> mockWSW = new Mock<IWebSecurityWrapper>();

        mockUserTeamRepository.Setup(mr => mr.GetAllUserTeams()).Returns(userteams);
        mockUserTeamRepository.Setup(mr => mr.GetUserTeam(0,1,1)).Returns(ut);
        mockUserTeamRepository.Setup(mr => mr.CreateUserTeam(1, 1));

        //Arrange
        GameController Controller = new GameController(mockGameRepository.Object, mockUserTeamRepository.Object, mockWSW.Object);

       // This didn't work
        //HttpContextFactory.SetFakeAuthenticatedControllerContext(Controller);

        //Act
        RedirectToRouteResult result = Controller.JoinGame(1);

        Assert.AreEqual("Index", result.RouteValues["action"]);
    }

1 个答案:

答案 0 :(得分:2)

当你硬编码1时,你回来0的原因是因为这一行:

Mock<IWebSecurityWrapper> mockWSW = new Mock<IWebSecurityWrapper>();

你得到的IWebSecurityWrapper的版本是一个模拟(因为你注入了它)。添加

mockSW.Setup(x=>x.CurrentUserId).Returns(1);

应该得到你所需要的。因为我们现在告诉模拟器在被要求使用CurrentUserId时返回1

HttpContextFactory无法工作的原因是因为我已经看到HttpContextFactory实现处理控制器上的属性而我怀疑你对HttpContext的依赖是在WebSecurity类本身内部,因此你需要包装器。