0

I am trying to mock a Interface method, but I am getting this error:

System.NotSupportedException : Unsupported expression: obj => obj.ReadInStreamAsync(It.IsAny<Stream>(), It.IsAny<string>(), It.IsAny<IList<string>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())
Extension methods (here: ComputerVisionClientExtensions.ReadInStreamAsync) may not be used in setup / verification expressions.

This is my code:

var computerVisionClientMock = new Mock<IComputerVisionClient>();

computerVisionClientMock.Setup(obj => obj.ReadInStreamAsync(
                                    It.IsAny<Stream>(),
                                    It.IsAny<string>(),
                                    It.IsAny<IList<string>>(),
                                    It.IsAny<string>(),
                                    It.IsAny<string>(),
                                    It.IsAny<CancellationToken>()
                                    ).Result).Callback<Stream, string, IList<string>, string, string, CancellationToken>((var0, var1, var2, var3, var4, var5) => { Thread.Sleep(2000); });

This is the Interface's definition:

IComputerVisionClient Interface

Rafael Colucci
  • 6,018
  • 4
  • 52
  • 121
  • 1
    Per the error, you cannot mock extension methods. You can only mock the methods actually defined in the interface. It's likely the extension method calls `ReadInStreamWithHttpMessagesAsync` (though you'd have to decompile to be sure), so consider mocking that instead. – Jeroen Mostert Mar 01 '22 at 21:27
  • Does this answer your question? [Mocking of extension method result in System.NotSupportedException](https://stackoverflow.com/questions/57705394/mocking-of-extension-method-result-in-system-notsupportedexception) – Andrew McClement Mar 02 '22 at 01:26

0 Answers0