views:

30

answers:

1

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?

+3  A: 

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

mockServiceBus.Setup(x => x.Subscribe(It.IsAny<ISubscribeTo<SomeType>>()));
adrift
I feel really, really stupid now that *that* turned out to be the problem. Haha. Oh well, just one of those days. Thank you, that was it!
unforgiven3
It's not that I'm new to Mock, I was just expecting it to be some crazy generics issue that Mock somehow didn't support :-) haha
unforgiven3
No worries, I only knew it because I've done the same thing. More than once :P
adrift