I am working on creating behavior related to the StackExchange.Redis library, but I cannot figure out how to properly mock the private classes that it uses. A specific example in my code: I am doing something like this:
var cachable = command as IRedisCacheable; if (_cache.Multiplexer.IsConnected == false) { _logger.Debug("Not using the cache because the connection is not available"); cacheAvailable = false; } else if (cachable == null) {
The key line has _cache.Multiplexer.IsConnected, where I check to make sure that I have a valid connection before using the cache. So in my tests, I want this behavior to be related to something like this:
_mockCache = new Mock<IDatabase>(); _mockCache.Setup(cache => cache.Multiplexer.IsConnected).Returns(false);
However, although this code compiles just fine, I get this error when running the test:

I also tried to make fun of the multiplexer class itself and provided this for my mocking cache, but I came across the fact that the multiplexer class is sealed:
_mockCache = new Mock<IDatabase>(); var mockMultiplexer = new Mock<ConnectionMultiplexer>(); mockMultiplexer.Setup(c => c.IsConnected).Returns(false); _mockCache.Setup(cache => cache.Multiplexer).Returns(mockMultiplexer.Object);
... but this leads to an error:

Ultimately, I want to control whether this property is true or false in my tests, so is there a proper way to do something like this?
source share