title: Mock IMemoryCache with Moq throwing exception
body: <p>I'm trying to mock <code>IMemoryCache</code> with Moq. I'm getting this error:</p>
  
  <blockquote>
    <p>An exception of type 'System.NotSupportedException' occurred in
    Moq.dll but was not handled in user code</p>
    
    <p>Additional information: Expression references a method that does not
    belong to the mocked object: x => x.Get&lt;String>(It.IsAny&lt;String>())</p>
  </blockquote>
  
  <p>My mocking code:</p>
  
  <pre><code>namespace Iag.Services.SupplierApiTests.Mocks
  {
      public static class MockMemoryCacheService
      {
          public static IMemoryCache GetMemoryCache()
          {
              Mock&lt;IMemoryCache&gt; mockMemoryCache = new Mock&lt;IMemoryCache&gt;();
              mockMemoryCache.Setup(x =&gt; x.Get&lt;string&gt;(It.IsAny&lt;string&gt;())).Returns("");&lt;---------- **ERROR**
              return mockMemoryCache.Object;
          }
      }
  }
  </code></pre>
  
  <p>Why do I get that error?</p>
  
  <p>This is the code under test:</p>
  
  <pre><code>var cachedResponse = _memoryCache.Get&lt;String&gt;(url);
  </code></pre>
  
  <p>Where <code>_memoryCache</code> is of type <code>IMemoryCache</code></p>
  
  <p>How do I mock the <code>_memoryCache.Get&lt;String&gt;(url)</code> above and let it return null?</p>
  
  <p><strong>Edit</strong>: How would I do the same thing but for  <code>_memoryCache.Set&lt;String&gt;(url, response);</code>? I don't mind what it returns, I just need to add the method to the mock so it doesn't throw when it is called.</p>
  
  <p>Going by the answer for this question I tried:</p>
  
  <pre><code>mockMemoryCache
      .Setup(m =&gt; m.CreateEntry(It.IsAny&lt;object&gt;())).Returns(null as ICacheEntry);
  </code></pre>
  
  <p>Because in the memoryCache extensions it shows that it uses <code>CreateEntry</code> inside <code>Set</code>. But it is erroring out with "object reference not set to an instance of an object".</p>