I'm trying to instantiate a class that handles a bunch of functionality, and is constructed using dependency injection. I mock two of my parameters, and the third is an in-memory database context. Now when asserting, it's throwing an error on the mockClientProxyParticipants.Verify()
line:
System.NotSupportedException : Invalid verify on an extension method:
proxy => proxy.SendAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())
It is virtually impossible to not call the function, and calling Verify()
with Times.Never()
as parameter doesn't change anything.
I tried setting up the Linq argument of Verify()
using normal strings like for mockClientProxyResults.Verify()
, I tried using It.Is<string>(s => s == "some string")
and I tried It.IsAny<string>()
.
I honestly don't understand what's wrong right now. Here's my test code, let me know if you want to see more:
var mockHub = new Mock<IHubContext<VoteHub>>();
var mockClients = new Mock<IHubClients>();
var mockClientProxyParticipants = new Mock<IClientProxy>();
var mockClientProxyResults = new Mock<IClientProxy>();
mockClients.Setup(clients => clients.Group("Participants")).Returns(mockClientProxyParticipants.Object);
mockClients.Setup(clients => clients.Group("Results")).Returns(mockClientProxyResults.Object);
mockHub.Setup(hub => hub.Clients).Returns(mockClients.Object);
var activeQuestionManagerMock = new Mock<IActiveQuestionManager>();
activeQuestionManagerMock.Setup(x => x.GetActiveQuestion())
.Returns(new ActiveQuestionModel(options));
QuestionHandler qm = new QuestionHandler(new VoteDbContext(options), mockHub.Object, activeQuestionManagerMock.Object);
//Act
qm.Ask(question);
//Assert
mockClientProxyParticipants.Verify(proxy => proxy.SendAsync(It.IsAny<string>(/*s => s == "AskQuestion"*/), It.IsAny<string>(/*s => s == qwaJSON*/), It.IsAny<CancellationToken>()), Times.Once());
mockClientProxyResults.Verify(proxy => proxy.SendAsync("UpdateCurrentQuestionProgress", It.IsAny<ActiveQuestionModel>(), default(CancellationToken)), Times.Once());
Turns out I misunderstood the error message and the issue is that IClientProxy.SendAsync()
is an extension method, and Moq apparently doesn't like that. It's possible to use SendCoreAsync()
instead, but you'll always have to pass an array as parameter, even if you want to send just one object or don't want to send any data at all.
credit to NKosi (see question comments)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With