title: Moq Controller to ControllerBase convert error
body: <p>I am getting this error when I am trying to use moq so that I can access application variables. In this case the application variable is <code>ConnectionString</code> with a value of <code>TheConnectionString</code>. I need the value to be available in the <code>GetCompanyList()</code> because the value is used inside <code>accountService</code> class</p>
  
  <p>The Error is :</p>
  
  <p><code>Error  5   Argument 3: cannot convert from 'Webapp.AccountServiceController' to 'System.Web.Mvc.ControllerBase'</code></p>
  
  <pre><code>[TestMethod]
  public void TestGetCompanyList()
   {
       var accountController = new AccountServiceController();
       var context = new Mock&lt;HttpContextBase&gt;();
       var application = new Mock&lt;HttpApplicationStateBase&gt;();
       var request = new Mock&lt;HttpRequestBase&gt;();
  
       application.Setup(m =&gt; m.Add("ConnectionString","TheConnectionString");
       request.SetupGet(x =&gt; x.Headers).Returns(new System.Net.WebHeaderCollection { { "X-Requested-With", "XMLHttpRequest" } });
       context.SetupGet(ctx =&gt; ctx.Request).Returns(request.Object);
       context.SetupGet(ctx =&gt; ctx.Application).Returns(application.Object);
  
       accountController.ControllerContext = new ControllerContext(context.Object, new RouteData(), accountController); //Error here
  
       CompanyInput cInput = new CompanyInput();
       cInput.IssuerName = "Be";
       cInput.Ticker = "BR";
       var result = accountController.GetCompanyList(cInput) as IEnumerable&lt;CompanyListResult&gt;;
       Assert.IsNotNull(result);
   }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public class AccountServiceController : ApiController
  {
      public AccountServiceFacade accoutService;
      public AccountServiceController()
      {
          accoutService = new AccountServiceFacade();
      }
  
      public AccountServiceController(AccountServiceFacade facade)
      {
          accoutService = facade;
      }
  
      [System.Web.Http.HttpPost]
      public dynamic GetCompanyList([FromBody]CompanyInput cInput)
      {
          IEnumerable&lt;CompanyListResult&gt; companyList = accoutService.GetCompanyList(cInput);
          return companyList;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <pre><code> [TestMethod]
      public void TestGetCompanyList()
      {
          var controller = new AccountServiceController();
          var config = new HttpConfiguration();
          var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/test");
          var route = config.Routes.MapHttpRoute("DFS", "api/{controller}/{id}");
          var routeData = new HttpRouteData(route, new HttpRouteValueDictionary
          {
              {
                  "ConnectionString", "TheConnectionString" 
  
              },
              {
                  "Username", "TheUserName"
              }
          });
          controller.ControllerContext = new HttpControllerContext(config, routeData, request);
          controller.Request = request;
          controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
  
          CompanyInput cInput = new CompanyInput();
          cInput.IssuerName = "Be";
          cInput.Ticker = "BR";
          var result = controller.GetCompanyList(cInput) as IEnumerable&lt;CompanyListResult&gt;;
          Assert.IsNotNull(result);
  
          //Assert.IsNotNull(result.IssueTicker);
      }
  </code></pre>
  
  <p>Now when my code reaches this line(which is in class that gets called by the accountService)</p>
  
  <pre><code>static string _authUsername = HttpContext.Current.Application["Username"].ToString();
  </code></pre>
  
  <p>The test throws this error : <code>System.NullReferenceException: Object reference not set to an instance of an object.1</code></p>