title: How do I write a unit test when the system under test utilizes external static dependencies?
body: <p>I am creating unit test for my method</p>
  
  <pre><code>[Authorize]
  [HttpPost]
  public async Task&lt;JsonResult&gt; UpdateDisplayName(string displayname)
  {
      bool status = _myProfileService.UpdateDisplayName(displayname, SessionAdapter.Instance._userDetailsViewModel.id);
  
      if (status)
          SessionAdapter.Instance._userDetailsViewModel.display_name = displayname;
  
      return Json(new { status = status, displayname = displayname }, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>and my test method is </p>
  
  <pre><code>[TestMethod]
  public async Task UpdateDisplayName_Test()
  {
      //Arrange
      var controller = new HomeController(userServiceMock.Object, myProfileServiceMock.Object);
  
      string displayName = "display";
      const string expected = "{ status = False, displayname = display }";
      myProfileServiceMock.Setup(m =&gt; m.UpdateDisplayName(It.IsAny&lt;string&gt;(), 1)).Returns(false);
  
      //var controllerContextMock = new Mock&lt;ControllerContext&gt;();
  
      //Act
      var result = await controller.UpdateDisplayName(displayName) as JsonResult;
  
      //Assert
      Assert.AreEqual(expected, result.Data.ToString());
  }
  </code></pre>
  
  <p>My Session Info Class is below, this class i am using in session adapter</p>
  
  <pre><code>  public  class SessionInfo
      {
          public string Id { set; get; }
          public string Email { set; get; }
          public string UserName { set; get; }
          public UserDetailsViewModel _userDetailsViewModel { set; get; }
          public string permission { set; get; }
  
          //public string organization { set; get; }
          public OrganizationViewModels Organization { set; get; }
          public List&lt;UserTeamModels&gt; teams { set; get; }
          public string status { set; get; }
          public string role { set; get; }
          public List&lt;string&gt; roles { set; get; }
      }
  </code></pre>
  
  <p>I am unable to instantiate <code>SessionAdapter</code>. How can I unit test this?</p>
  
  <p>My Interface and sessionadapter class were looks like below</p>
  
  <pre><code>public interface ISessionAdapterService
      {
          void Clear();
          void Abandon();
          bool DoesSessionExists { get; }
          SessionInfo Instance { get; set; } 
      }
  
  
      public class SessionAdapterService : ISessionAdapterService
      {
          private string sessionKey = "sessionInfoKey";
  
          public bool DoesSessionExists
          {
              get
              {
                  return HttpContext.Current.Session[sessionKey] == null ? false : true;
              }
  
          }
  
          public SessionInfo Instance
          {
              get
              {
                  return HttpContext.Current.Session[sessionKey] == null ? null : (SessionInfo)HttpContext.Current.Session[sessionKey];
              }
              set
              {
                  HttpContext.Current.Session[sessionKey] = value;
              }
          }
  
  
  
          public void Abandon()
          {
  
              HttpContext.Current.Session.Abandon();
              HttpContext.Current.Session[sessionKey] = null;
          }
  
          public void Clear()
          {
              HttpContext.Current.Session.Clear();
  
          }
      }
  </code></pre>
  
  <p>My test case is same as the answer below</p>
  
  <pre><code>[TestMethod]
  public async Task UpdateDisplayName_Test() {
      //Arrange
      var mySessionAdaptorService = new Mock&lt;ISessionAdaptorService&gt;();
  
      var controller = new HomeController(userServiceMock.Object, myProfileServiceMock.Object, mySessionAdaptorService.Object);
  
      var displayName = "display";
      var status = false;
      var id = 1;
      myProfileServiceMock.Setup(m =&gt; m.UpdateDisplayName(It.IsAny&lt;string&gt;(), id)).Returns(status);
      mySessionAdaptorService.Setup(m =&gt; m.Instance.Id).Returns(id);
  
      //Act
      var result = await controller.UpdateDisplayName(displayName) as JsonResult;
  
      //Assert
      Assert.IsNotNull(result); 
  }
  </code></pre>
  
  <p>Code Update. Please find the below code i used for SessionAdapter class and ISessionAdapter Interface and also implementation.please give your suggestions is this correct way.</p>
  
  <pre><code>     public interface ISessionInfo
          {
              string Id { set; get; }
              string Email { set; get; }
              string UserName { set; get; }
              UserDetailsViewModel _userDetailsViewModel { set; get; }
              string permission { set; get; }
  
              OrganizationViewModels Organization { set; get; }
              List&lt;UserTeamModels&gt; teams { set; get; }
              string status { set; get; }
              string role { set; get; }
              List&lt;string&gt; roles { set; get; }
          }
  
          public  class SessionInfo : ISessionInfo
          {
              public string Id { set; get; }
              public string Email { set; get; }
              public string UserName { set; get; }
              public UserDetailsViewModel _userDetailsViewModel { set; get; }
              public string permission { set; get; }
  
              //public string organization { set; get; }
              public OrganizationViewModels Organization { set; get; }
              public List&lt;UserTeamModels&gt; teams { set; get; }
              public string status { set; get; }
              public string role { set; get; }
              public List&lt;string&gt; roles { set; get; }
          }
  
   public interface ISessionAdapter
      {
          void Clear();
          void Abandon();
          bool DoesSessionExists { get; }
          ISessionInfo Instance { get; set; }
  
      }
  
  
      public class SessionAdapter : ISessionAdapter
      {
          private string sessionKey = "sessionInfoKey";
  
          public bool DoesSessionExists
          {
              get
              {
                  return HttpContext.Current.Session[sessionKey] == null ? false : true;
              }
          }
  
          public ISessionInfo Instance
          {
              get
              {
                  return HttpContext.Current.Session[sessionKey] == null ? null : (SessionInfo)HttpContext.Current.Session[sessionKey];
              }
              set
              {
                  HttpContext.Current.Session[sessionKey] = value;
              }
          }
  
          public void Abandon()
          {
  
              HttpContext.Current.Session.Abandon();
              HttpContext.Current.Session[sessionKey] = null;
          }
  
          public void Clear()
          {
              HttpContext.Current.Session.Clear();
  
          }
  </code></pre>