Search code examples
c#unit-testingmoq

how to fix system.NotsupportedException moq use, trying to moq an object


I am trying to mock an object

_logger = new Mock<ILogger>();
_sender = new Mock<ServiceBusMessageSender>();

the error is thrown in this line:

_sender.Setup(x => x.SendMessageToQueue(It.IsAny<MyClass>(), It.IsAny<ILogger>())).ReturnsAsync(true);

and this is the signature of the method that I can't setup:

public async Task<bool> SendMessageToQueue(MyClass msg, ILogger log);

I don't get why this method can't be overriden.

I was not expecting to get an error, I tried using mockbehaviour.loose, verifiable


Solution

  • The answer was given by @ProgrammingLlama in the comments, I added virtual to the method signature.

    The problem was simple, I didn't realized that most of the examples that I looked were mocking an interface and not a class, therefore the method could be overridden, not in a class