如何在 C# MVC 5.0 中执行表单身份验证测试

How to perform Form Authentication testing in C# MVC 5.0

我试过很多,也搜索过。我得到的链接很少,但我没有成功解决这个问题。

没有生成任何虚拟表单验证,在单元测试(c#-mvc)项目中我想执行表单验证。

我使用过如下的接口方法

    private readonly IAuthenticationProvider _authenticationProvider

    public AccountController(IAuthenticationProvider authenticationProvider)
    {
        _authenticationProvider = authenticationProvider;
    }

    public interface IAuthenticationProvider
    {
        void SignOut();
        void SetAuthCookie(string username, bool remember);
        void RedirectFromLoginPage(string username, bool createPersistentCookie);
    }


    public class FormsAuthWrapper : IAuthenticationProvider
    {
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
        public void SetAuthCookie(string username, bool remember)
        {
            FormsAuthentication.SetAuthCookie(username, remember);
        }
        public void RedirectFromLoginPage(string username, bool createPersistentCookie)
        {
            FormsAuthentication.RedirectFromLoginPage(username, createPersistentCookie);
        }
    }

待登录

   public ActionResult Login(LoginViewModel model, string returnurl)
   {
      _authenticationProvider.SetAuthCookie(model.username, false)
   }

是否有其他方式初始化表单验证对象。 我仍然收到错误消息:未将对象引用设置为对象的实例。

因为 _authenticationProvider 为 null 且表单验证未初始化

它说使用 new 关键字创建对象实例

StackTrace 正在跟踪..

   at System.Web.Security.CookielessHelperClass.UseCookieless(HttpContext context, Boolean doRedirect, HttpCookieMode cookieMode)
   at System.Web.Security.FormsAuthentication.SetAuthCookie(String userName, Boolean createPersistentCookie, String strCookiePath)
   at System.Web.Security.FormsAuthentication.SetAuthCookie(String userName, Boolean createPersistentCookie)
   at Test.Controllers.AccountController.Login(LoginViewModel model, String returnurl) in d:\projects\Test\Test\Controllers\AccountController.cs:line 81
   at Test.Tests.Controllers.CIControllerTest.TestSetup() in d:\projects\Test\Test.Tests\Controllers\CIControllerTest.       cs:line 47

对于 IAuthenticationProvider 的初始化,请在 AccountController 中使用以下内容。

private readonly IAuthenticationProvider _authenticationProvider = new FormsAuthWrapper();

之后在 TestController 中。如下创建新的测试 Warrper class。

public class FormsAuthWrapperTest : IAuthenticationProvider
{
    public void SignOut()
    {
       // MockFormsAuthentication.SignOut();
       // Mock your Authentication here
    }
    public void SetAuthCookie(string username, bool remember)
    {
       //MockFormsAuthentication.SetAuthCookie(username, remember);
       //Mock your Authentication here

    }
    public void RedirectFromLoginPage(string username, bool createPersistentCookie)
    {
        MockFormsAuthentication.RedirectFromLoginPage(username, createPersistentCookie);
       //Mock your Authentication here
    }
}

终于在TestController中进行测试了。 像下面这样创建帐户控制器对象。

AccountController accountController = new AccountController(new FormsAuthWrapperTest());

现在您可以使用测试包装器 class 进行测试,并使用包装器进行直播。