title: How to Mock Session variables in ASP.net core unit testing project?
body: <p><strong>How to Mock Session variables in ASP.net core unit testing project?</strong></p>
  <p><strong>1) I have created a mock object of a session.</strong></p>
  <pre><code>Mock&lt;HttpContext&gt; mockHttpContext = new Mock&lt;HttpContext&gt;();
  Mock&lt;ITestSession&gt; mockSession = new Mock&lt;ISession&gt;().As&lt;ITestSession&gt;();
  </code></pre>
  <p><strong>2) Setup GetString() MEthod</strong></p>
  <pre><code>mockSession.Setup(s =&gt; s.GetString(&quot;ModuleId&quot;)).Returns(&quot;1&quot;);
  </code></pre>
  <p><strong>3) created controllerContext and assigned mockhttpContext object</strong></p>
  <pre><code>controller.ControllerContext.HttpContext = mockHttpContext.Object; 
  </code></pre>
  <p><strong>4) Trying to read from a controller.</strong></p>
  <pre><code>HttpContext.Session.GetString(&quot;ModuleId&quot;)
  </code></pre>
  <p>Whereas I get a null value of &quot;ModuleId&quot;. Please help me to mock session GetString() method</p>
  <p><strong>Example:</strong></p>
  <pre><code>        //Arrange
          //Note: Mock session 
          Mock&lt;HttpContext&gt; mockHttpContext = new Mock&lt;HttpContext&gt;();
          Mock&lt;ITestSession&gt; mockSession = new Mock&lt;ISession&gt;().As&lt;ITestSession&gt;();
          //Cast list to IEnumerable
          IEnumerable&lt;string&gt; sessionKeys = new string[] { };
          //Convert to list.
          List&lt;string&gt; listSessionKeys = sessionKeys.ToList();
          listSessionKeys.Add(&quot;ModuleId&quot;);
          sessionKeys = listSessionKeys;
          mockSession.Setup(s =&gt; s.Keys).Returns(sessionKeys);
          mockSession.Setup(s =&gt; s.Id).Returns(&quot;89eca97a-872a-4ba2-06fe-ba715c3f32be&quot;);
          mockSession.Setup(s =&gt; s.IsAvailable).Returns(true);
          mockHttpContext.Setup(s =&gt; s.Session).Returns(mockSession.Object);
       mockSession.Setup(s =&gt; s.GetString(&quot;ModuleId&quot;)).Returns(&quot;1&quot;);         
  
          //Mock TempData
          var tempDataMock = new Mock&lt;ITempDataDictionary&gt;();
          //tempDataMock.Setup(s =&gt; s.Peek(&quot;ModuleId&quot;)).Returns(&quot;1&quot;);
  
          //Mock service
          Mock&lt;ITempServices&gt; mockITempServices= new Mock&lt;ITempServices&gt;();
          mockITempServices.Setup(m =&gt; m.PostWebApiData(url)).Returns(Task.FromResult(response));
  
          //Mock Management class method
          Mock&lt;ITestManagement&gt; mockITestManagement = new Mock&lt;ITestManagement&gt;();
          mockITestManagement .Setup(s =&gt; s.SetFollowUnfollow(url)).Returns(Task.FromResult(response));
  
          //Call Controller method
          TestController controller = new TestController (mockITestManagement .Object, appSettings);
          controller.ControllerContext.HttpContext = mockHttpContext.Object;            
          controller.TempData = tempDataMock.Object;
  
          //Act
          string response = await controller.Follow(&quot;true&quot;);
  
          // Assert
          Assert.NotNull(response);
          Assert.IsType&lt;string&gt;(response);
   
  </code></pre>