title: Unit testing controller using MOQ . How to mock httpcontext
body: <p>I am trying to test my Account controller by using <code>Moq</code> here is what i have done </p>
  
  <p><strong>Controller</strong>  </p>
  
  <pre><code>   private readonly IWebSecurity _webSecurity;
      public AccountController(IWebSecurity webSecurity)
      {
          this._webSecurity = webSecurity;
      }
      public ActionResult Login(LoginModel model, string returnUrl)
      {
          if (ModelState.IsValid &amp;&amp; _webSecurity.login(model))
          {
              return RedirectToLocal(returnUrl);
          }
  
          // If we got this far, something failed, redisplay form
          ModelState.AddModelError("", "The user name or password provided is incorrect.");
          return View(model);
      }
      private ActionResult RedirectToLocal(string returnUrl)
      {
          if (Url.IsLocalUrl(returnUrl))
          {
              return Redirect(returnUrl);
          }
          else
          {
              return RedirectToAction("Index", "Home");
          }
      }
  </code></pre>
  
  <p><strong>IWebSecurity</strong></p>
  
  <pre><code>public interface IWebSecurity
  {
      bool login(LoginModel model);
  }
  
  
  public class WebSecurity : IWebSecurity
  {
      public bool login(LoginModel model)
      {
          return WebMatrix.WebData.WebSecurity.Login(model.UserName, model.Password, model.RememberMe);
      }
  }
  </code></pre>
  
  <p><strong>MyTestClass</strong></p>
  
  <pre><code>[AfterScenario]
      public void OnAfterScenario() {
          mockRepository.VerifyAll();
      }
  
      LoginModel loginModel;
      AccountController _controller;
  
      #region Initializing Mock Repository
  
      readonly Mock&lt;IWebSecurity&gt; mockRepository = new Mock&lt;IWebSecurity&gt;(MockBehavior.Loose);
      ViewResult viewResult;
  
      #endregion
  
      [Given]
      public void Given_Account_controller()
      {
          _controller = new AccountController(mockRepository.Object);
      }
  
      [When]
      public void When_login_is_called_with_LoginModel(Table table)
      {
           loginModel = new LoginModel
              {
                  UserName = table.Rows[0][1],
                  Password = table.Rows[1][1]
              };
           mockRepository.Setup(x =&gt; x.login(loginModel)).Returns(true);
           viewResult = (ViewResult)_controller.Login(loginModel, "/");
      }
  
      [Then]
      public void Then_it_should_validate_LoginModel()
      {
         Assert.IsTrue(_controller.ModelState.IsValid);
      }
  
      [Then]
      public void Then_it_should_return_default_view()
      {
          Assert.AreEqual(viewResult.ViewName, "Index");
      }
  </code></pre>
  
  <p>But my test is failing and its giving expection when if come to <code>Url.IsLocal</code> in <code>Redirect to Local</code> method . so i think here is should mock my httpcontextbase and httpcontextrequestbase . </p>
  
  <p>But don't know how to mock that . </p>
  
  <p>Thanks in advance </p>