Rob
Rob

Reputation: 26324

Compile error when trying to mock a generic method with Moq

I have a method I'd like to mock:

public interface IServiceBus
{
    void Subscribe<T>(ISubscribeTo<T> subscriber) where T : class;
}

For the sake of this example, T can be something called SomeType. Now, I'd like to mock this, like so:

var mockServiceBus = new Mock<IServiceBus>();
mockServiceBus.Setup(x => x.Subscribe(It.IsAny<ISubscribeTo<SomeType>>));

However, when I try this, I get this compile error:

Error 65
The type arguments for method 'ServiceBus.IServiceBus.Subscribe(Messaging.ISubscribeTo)' cannot be inferred from the usage.
Try specifying the type arguments explicitly.

I'm not sure how to work around this error. Any ideas? Or is this behavior not possible to mock with Moq?

Upvotes: 1

Views: 1444

Answers (1)

Jeff Ogata
Jeff Ogata

Reputation: 57783

try this (adding parentheses since It.IsAny<TValue> is a method):

mockServiceBus.Setup(x => x.Subscribe(It.IsAny<ISubscribeTo<SomeType>>()));

Upvotes: 6

Related Questions