1

我很难使用 NUnit 为我的 MVC4 应用程序创建单元测试。现在我只特别关注这个测试,它将用户登录或创建它们,如果它们不存在,然后将它们登录。这是测试:

[Test]
public void LoginValidUser()
{
       //Start up the DB connection
        App_Start_DB();

        //Setup our default test user
        string UserName = "LocalTestUser@penrouse.com";
        string Password = "books";
        bool LoginWorked = false;

        //check and see if our test user exists
        if (!WebSecurity.UserExists(UserName))
        {
            //If not, create them
            WebSecurity.CreateUserAndAccount(UserName, Password, new
            {
                Name = "Local Test User",
                IsPromotional = true,
                IsAllowShare = true
            });

            //Log them in
            LoginWorked = WebSecurity.Login(UserName, Password);
        }
        else
        {
            //This user already exists, just log them in     
            LoginWorked = WebSecurity.Login(UserName, Password);
        }

        Assert.IsTrue(LoginWorked);
        Trace.WriteLine("Login Valid User Result : " + LoginWorked.ToString());
}

问题是每次我尝试WebSecurity.Login()时,我都会得到一个空引用异常,并且堆栈跟踪指向:

System.Web.Security.FormsAuthentication.SetAuthCookie(String userName, Boolean createPersistentCookie, String strCookiePath);

在登录尝试之前直接调用该方法不会改变行为。因此,我有两个问题:

  • 有没有更好的方法来测试 SimpleMembership 的这些部分?
  • 如果没有,是否有一种好方法可以覆盖或模拟 AuthCookie,以便在以这种方式测试时登录将起作用?

任何帮助\见解将不胜感激。

4

0 回答 0