0

so I'm trying to Mock (Redis) IDistributedCache.GetStringAsync and IDistributedCache.SetStringAsync, but I understand we won't be able to Mock extension methods, however, we can mock the methods used by those extension methods to achieve our testing but sadly, I couldn't find what are the methods that have been used by GetStringAsync/SetStringAsync even in official docs, so I'd appreciate if someone could help me out here, on how to mock SetStringAsync and GetStringAsync, I've added my current test cases below

Edit: When I run the current test case I'm getting the below error

Unsupported expression: x => x.GetStringAsync(key, CancellationToken) Extension methods (here: DistributedCacheExtensions.GetStringAsync) may not be used in setup / verification expressions.

public async Task Set_Records_with_key()
{
    // Arrange
    var key = "key";
    var value = "cdc";
    var cache = _builder.DistributedCache;

    var sut = _builder.Build();

    // Act
    await sut.GetRecordAsync<string>(key);

    // Assert
    cache.Verify(x => x.SetStringAsync(key, value,default));
}


public async Task GetRecords_with_key()
{
    // Arrange
    var key = "key";
    var cache = _builder.DistributedCache;

    var sut = _builder.Build();

    // Act
    await sut.GetRecordAsync<string>(key);

    // Assert
    cache.Verify(x => x.GetStringAsync(key, default));
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Noob
  • 25
  • 6

0 Answers0