4

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

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

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

然后我找到了这个Mocking WebSecurity 提供程序

我为 websecurity 创建了一个包装器接口和类,并模拟了包装器并将 websecuritywrapper 注入到游戏控制器中。这解决了 httpContext (虽然我现在不明白为什么它有效而 HttpContextFactory 没有),但是 websecuritywrapper 返回的 CurrentUserId 始终为 0。即使我将它硬编码为 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"]);
    }

public class WebSecurityWrapper : IWebSecurityWrapper
{
    public int CurrentUserId{ get { return WebSecurity.CurrentUserId; }}
    public string CurrentUserName { get { return "admin_user"; } } // WebSecurity.CurrentUserName;
    public bool HasUserId { get { return WebSecurity.HasUserId; } }
    public bool Initialized { get { return WebSecurity.Initialized; } }
    public bool IsAuthenticated { get { return WebSecurity.IsAuthenticated; } }
    public bool ChangePassword(string userName, string currentPassword, string newPassword){return WebSecurity.ChangePassword(userName, currentPassword, newPassword);}
    public bool ConfirmAccount(string accountConfirmationToken) { return WebSecurity.ConfirmAccount(accountConfirmationToken); }
    public bool ConfirmAccount(string userName, string accountConfirmationToken) { return WebSecurity.ConfirmAccount(userName,accountConfirmationToken); }
    public string CreateAccount(string userName, string password, bool requireConfirmationToken = false) { return WebSecurity.CreateAccount(userName, password, requireConfirmationToken = false); }
    public string CreateUserAndAccount(string userName, string password, object propertyValues = null, bool requireConfirmationToken = false) { return WebSecurity.CreateUserAndAccount(userName, password, propertyValues = null, requireConfirmationToken = false); }
    public string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow = 1440) { return WebSecurity.GeneratePasswordResetToken(userName, tokenExpirationInMinutesFromNow = 1440); }
    public DateTime GetCreateDate(string userName) { return WebSecurity.GetCreateDate(userName); }
    public DateTime GetLastPasswordFailureDate(string userName){ return WebSecurity.GetLastPasswordFailureDate(userName); }
    public DateTime GetPasswordChangedDate(string userName) { return WebSecurity.GetPasswordChangedDate(userName); }
    public int GetPasswordFailuresSinceLastSuccess(string userName) { return WebSecurity.GetPasswordFailuresSinceLastSuccess(userName);}
    public int GetUserId(string userName){ return WebSecurity.GetUserId(userName);}
    public int GetUserIdFromPasswordResetToken(string token) { return WebSecurity.GetUserIdFromPasswordResetToken(token); }
    public void InitializeDatabaseConnection(string connectionStringName, string userTableName, string userIdColumn, string userNameColumn, bool autoCreateTables) { WebSecurity.InitializeDatabaseConnection(connectionStringName, userTableName, userIdColumn, userNameColumn, autoCreateTables); }
    public void InitializeDatabaseConnection(string connectionString, string providerName, string userTableName, string userIdColumn, string userNameColumn, bool autoCreateTables) { WebSecurity.InitializeDatabaseConnection(connectionString, providerName, userTableName, userIdColumn, userNameColumn, autoCreateTables); }
    public bool IsAccountLockedOut(string userName, int allowedPasswordAttempts, int intervalInSeconds) { return WebSecurity.IsAccountLockedOut(userName, allowedPasswordAttempts, intervalInSeconds); }
    public bool IsAccountLockedOut(string userName, int allowedPasswordAttempts, TimeSpan interval) { return WebSecurity.IsAccountLockedOut(userName, allowedPasswordAttempts, interval); }
    public bool IsConfirmed(string userName){ return WebSecurity.IsConfirmed(userName); }
    public bool IsCurrentUser(string userName) { return WebSecurity.IsCurrentUser(userName); }
    public bool Login(string userName, string password, bool persistCookie = false) { return WebSecurity.Login(userName, password, persistCookie = false); }
    public void Logout() { WebSecurity.Logout(); }
    public void RequireAuthenticatedUser() { WebSecurity.RequireAuthenticatedUser(); }
    public void RequireRoles(params string[] roles) { WebSecurity.RequireRoles(roles); }
    public void RequireUser(int userId) { WebSecurity.RequireUser(userId); }
    public void RequireUser(string userName) { WebSecurity.RequireUser(userName); }
    public bool ResetPassword(string passwordResetToken, string newPassword) { return WebSecurity.ResetPassword(passwordResetToken, newPassword); }
    public bool UserExists(string userName) { return WebSecurity.UserExists(userName); }
}
4

1 回答 1

2

硬编码 1 时返回 0 的原因是因为这一行:

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

您获得的 IWebSecurityWrapper 版本是模拟的(因为您是这样注入的)。添加

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

应该给你你需要的。因为我们现在告诉模拟在询问 CurrentUserId 时返回 1

HttpContextFactory 不起作用的原因是因为我看到的 HttpContextFactory 实现处理控制器上的属性,我怀疑您对 HttpContext 的依赖在 WebSecurity 类本身内部,因此您需要包装器。

于 2013-06-14T06:53:06.503 回答