C#, Moq and generic methods

Associate
Joined
15 Dec 2008
Posts
126
Code:
public interface IDoStuff 
{
    void DoStuff<T>(T item);
}

[TestFixture]
public class MyTestClass
{
    [SetUp]
    public void Setup() 
    {
        _doStuff = new Mock<IDoStuff>();
        _doStuff.Setup(x => x.DoStuff(It.IsAny<object>)).Callback<object>(...);
    }
}

The callback will only be called when T is object, not when T is another class (that inherits object). Is there a way of getting a callback to run, whatever the generic type is, as long as it inherits T rather than is T?

Im doing one setup per expected type instead, which isn't very elegant, or flexible (copy/paste coding and need to fix the test every time another case of T turns up...)

Edit: Solution seems to be a good old-fashined stub for this, but I'd still like to know if it's doable with Moq.
 
Last edited:
Asserting that "stuff has been done to item", without caring what type the item is. Just so the setup doesn't need to be copy-pasted throgh a bunch of tests with only the type parameter differing.

The callback stuff is just tacked on to figure out why why the tests were failing (setup not being run).
 
You wouldn't need to copy/paste it if you abstract the test class like so:

Code:
[TestFixture]
public class WhenCallingDoStuffWithObject : MyTestClass<object>
{
 // the tests
}
and use the generic as such:
Code:
public class MyTestClass<T>
{
    [SetUp]
    public void Setup() 
    {
        _doStuff = new Mock<IDoStuff>();
        _doStuff.Setup(x => x.DoStuff(It.IsAny<T>)).Callback<T>(...);
    }
}
 
That wouldn't work as it would make it impossible to setup multiple types that way. The methods I need to set up are potentially going to get called with a lot of different types in a test.

We're trying to retrofit testing into an old app as we're changing it now, which means we'll have to work around a lot of nasty test unfriendly stuff. This problems arise when we're trying to mock a temp storage class (there's two implementations, one stores/retrieves to HttpContext and session in the web, and some custom model in a workflow project). The interface has a couple of generic retrieve and store methods that just "needs to work" as a first step.

Right now Im just trying to get the thing to run in the the test project. Then add tests and assertions as we start pulling stuff apart and figure out what it's supposed to be doing. So it would be nice if I could make a "catch all"-setup for those methods that'll either return nothing/what has previously been set, and allow me to assert on them, or grab the arguments if I need to assert on them, without figuring out all the potential types up front.

It's pretty simple with a stub that just dumps everything into a dictionary and then assert on that, so it looks like that's what we'll be doing for now.
 
Last edited:
I guess it won't be pretty then.

Tried RhinoMocks? That has an "IgnoreArguments()" option that Moq doesn't support (Moq only supports It.IsAny<T>()).
 
Back
Top Bottom