0

I'm trying to mock the IMemoryCache interface.

The IMemoryCache interface looks like:

public interface IMemoryCache : IDisposable
{
    //...
    bool TryGetValue(object key, out object value);
    //...
}

And the ImemoryCache has an extension method (namespace Microsoft.Extensions.Caching.Memory of:

public static class CacheExtensions
{
    public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out TItem value);
}

The mocking process is:

AutoMock mock = AutoMock.GetLoose();

string access_token = Guid.NewGuid().ToString();

var memoryCacheMock = mock.Mock<IMemoryCache>();
memoryCacheMock
    .Setup(x => x.TryGetValue(It.IsAny<object>(), out access_token))
    .Returns(true);

I'm trying to mock the TryGetValue method of the interface (not the extension method), but it automatically refers to the extension (it is not possible to mock an extension method).

When I hover the TryGetValue it show that it is referring to the extension: enter image description here

How to make the setup of TryGetValue refer to the overloaded interface method?

Edit:

Changing the string into object make it worked, but why? the string is a type of object.

Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91

0 Answers0