title: How to set the value of a query string in test method Moq
body: <p>I have the following controller action method and I am writing a unit test for this method</p>
  
  <pre><code>    try
      {
      if ( Session["token"] == null)
          {
              //checking whether the user has already given the credentials and got redirected by survey monkey by  checking the query string 'code'
              if (Request.QueryString["code"] != null)
              {
                  string tempAuthCode = Request.QueryString["code"];
                  Session["token"] = _surveyMonkeyService.GetSurveyMonkeyToken(ApiKey, ClientSecret, tempAuthCode, RedirectUri, ClientId);
              }
              else
              {
                  //User coming for the first time directed to authentication page
                  string redirectUrlToSurveyMonkeyAuthentication = _surveyMonkeyService.GetUrlToSurveyMonkeyAuthentication(RedirectUri, ClientId, ApiKey);
                  return Redirect(redirectUrlToSurveyMonkeyAuthentication);
              }
          }    
          //User is in the same session no need for token again showing surveys without authentication
          var model = _surveyService.GetSurveys(User.Identity.Name);
          if (model.Count == 0)
              return View(CSTView.NoSurveyTracker.ToString());
          return View(CSTView.Index.ToString(), model);
      }
      catch (Exception e)
      {
          return DisplayErrorView(e);//Even this returns a redirect method 
      }
  </code></pre>
  
  <p>And Here is one of the unit Test which I have written for it,</p>
  
  <pre><code>    [Test]
      public void GetIndexPage_Returns_View_With_ValidToken()
      {
          var mockControllerContext = new Mock&lt;ControllerContext&gt;();
          var mockSession = new Mock&lt;HttpSessionStateBase&gt;();
          mockSession.SetupGet(s =&gt; s["SurveyMonkeyAccessToken"]).Returns(SampleToken);
          mockSession.SetupGet(c =&gt; c["code"]).Returns(SampleTempAuthCode);
          mockControllerContext.Setup(p =&gt; p.HttpContext.Session).Returns(mockSession.Object);
          _surveyTrackerController.ControllerContext = mockControllerContext.Object;
          _surveyServiceMock.Setup(x =&gt; x.GetSurveys(TestData.TestData.SampleUserName)).Returns(SurveyTrackerList);
          var result = _surveyTrackerController.GetIndexPage();
          Assert.IsInstanceOf(typeof(ActionResult), result);
          Assert.AreEqual(((ViewResult)result).ViewName, "expected");
      }
  </code></pre>
  
  <p>When I am trying to run the test its throwing error: Object reference not set to an instance of object , and the line number shows to request.querystring , How to set the session variables in test methods, and Can anyone suggest me what is the proper way to check a controller action return type. </p>