title: How Add test cookie to Request in C# unit test
body: <p>How I can add test cookie to request so I can test my code from Unit test. Consider a code like this:</p>
  
  <pre><code>public ActionResult Dashboard()
  {
      if (Request.Cookies["usercookie"] == null)
      {
          return RedirectToAction("Index");
      }
      return View();
  }
  </code></pre>
  
  <p>I mock everything, but  I don't know how I can add something to cookie so this line <code>Request.Cookies["usercookie"]</code> not return null. As now it is null and returning me this error:</p>
  
  <blockquote>
    <p>{"Object reference not set to an instance of an object."}</p>
  </blockquote>
  
  <p>This is one of my unit test methods:</p>
  
  <pre><code>[TestMethod]
  [TestCategory("Unit")]
  public void Login_ShouldValidateUserAndLoginSuccessfully()
  {
      using (var kernel = new NSubstituteMockingKernel())
      {
          // Setup the dependency incjection
          kernel.Load(new EntityFrameworkTestingNSubstituteModule());
  
          // Create test request data 
          var request = new LogInRequest { UserName = "test", Password = "test" };
  
          var fakeResponseHandler = new FakeResponseHandler();
          fakeResponseHandler.AddFakeResponse(new Uri("http://localhost/test"), new HttpResponseMessage(HttpStatusCode.OK));
          ConfigurationManager.AppSettings["SearchApiBaseUrl"] = "http://test/internal";
          var server = new HttpServer(new HttpConfiguration(), fakeResponseHandler);
          var httpClient = new HttpClient(server);
  
          var fakeCookieManager = new FakeCookieManager();
          var authenticationService = Substitute.For&lt;IAuthenticationService&gt;();
          var newUser = Fake.GetNewUser(1);
          var newUserClaim = Fake.GetNewUserClaim(1, newUser.Id, "http://test/identity/claims/loans");
          authenticationService.GetUserByEmailPasswordAsync(request.UserName, request.Password).Returns(newUser);
          authenticationService.GetUserClaimByEmailAndPasswordAsync(request.UserName, request.Password).Returns(newUserClaim);
          var controller = new HomeController(httpClient, fakeCookieManager, null, authenticationService);
          Fake.SetFakeAuthenticatedControllerContext(controller);
          controller.HttpContext.Session["ReturnUrl"] = "/search"; 
          var result = controller.Login(request);
          Assert.IsNotNull(result);
      }
  }
  </code></pre>
  
  <p>This is a class in Fake for Httpcontext:</p>
  
  <pre><code>    public static HttpContextBase InitialiseFakeHttpContext(string url = "")
      {
          var HttpContextSub = Substitute.For&lt;HttpContextBase&gt;();
          var RequestSub = Substitute.For&lt;HttpRequestBase&gt;();
          var ResponseSub = Substitute.For&lt;HttpResponseBase&gt;();
          var serverUtilitySub = Substitute.For&lt;HttpServerUtilityBase&gt;();
          var itemsSub = Substitute.For&lt;IDictionary&gt;();
          HttpContextSub.Request.Returns(RequestSub);
          HttpContextSub.Response.Returns(ResponseSub);
          HttpContextSub.Server.Returns(serverUtilitySub);
          var cookie = Substitute.For&lt;HttpResponseBase&gt;();
          HttpContextSub.Response.Returns(cookie);
          return HttpContextSub;
      }
  </code></pre>