title: Unit testing a c# Method using Moq
body: <p>I'm new to unit testing and the language c sharp and I'm trying to unit test by mocking <code>ISessionStorageService</code> to test the <code>HasGlobaladminaccess</code>  function but my setup does not seem to work as expected.</p>
  <pre class="lang-cs prettyprint-override"><code>public class Context
  {
      public asyncTask&lt;bool&gt;HasGlobalAdminAccessAsync(Blazored.SessionStorage.ISessionStorageService sessionStorage)
      {
          return await HasAccessAsync(sessionStorage, Config.Constants.HAS_GLOBALE_ADMIN_ACCESS);
      }
  
      private async Task&lt;bool&gt; HasAccessAsync(Blazored.SessionStorage.ISessionStorageService sessionStorage, string sessionStorageKey)
      {
          ILogger _logger = Log.ForContext&lt;ContextHelpers&gt;();
  
          try
          {
              _logger.Debug(&quot;HasAccessAsync({sessionStorageKey})&quot;, sessionStorageKey);
              var access = await sessionStorage.GetItemAsync&lt;bool&gt;(sessionStorageKey);
              _logger.Debug(&quot;HasAccessAsync({sessionStorageKey})::access={access}&quot;, sessionStorageKey, access);
              return access;
          }
          catch (Exception ex)
          {
              _logger.Debug(&quot;HasAccessAsync({sessionStorageKey})::exception={ex}&quot;, sessionStorageKey, ex.Message);
              return false;
          }
      }
  }
  </code></pre>
  <p>My test method</p>
  <pre class="lang-cs prettyprint-override"><code>private readonly Mock&lt;ISessionStorageService&gt; MockStorage = new Mock&lt;ISessionStorageService&gt;();
  
  [Fact()]
  public async Task HasGlobalAdminAccessAsyncTest()
  {
      string guid = System.Guid.NewGuid().ToString();
      Context context = new Context();
      MockStorage.Setup(foo =&gt; foo.GetItemAsync&lt;bool&gt;(guid)).ReturnsAsync(true);
      var person = await context.HasGlobalAdminAccessAsync(MockStorage.Object);
      Assert.True(person , guid);
  }
  </code></pre>