97

I have an interface with a method as follows:

public interface IRepo
{
    IA<T> Reserve<T>();
}

I would like to mock the class that contains this method without having to specify Setup methods for every type it could be used for. Ideally, I'd just like it to return a new mock<T>.Object.

How do I achieve this?

It seems my explanation was unclear. Here's an example - this is possible right now, when I specify the T (here, string):

[TestMethod]
public void ExampleTest()
{
    var mock = new Mock<IRepo>();
    mock.Setup(pa => pa.Reserve<string>()).Returns(new Mock<IA<string>>().Object);
}

What I would like to achieve is something like this:

[TestMethod]
public void ExampleTest()
{
    var mock = new Mock<IRepo>();
    mock.Setup(pa => pa.Reserve<T>()).Returns(new Mock<IA<T>>().Object);
    // of course T doesn't exist here. But I would like to specify all types
    // without having to repeat the .Setup(...) line for each of them.
}

Some methods of the object under test might call reserve for three or four different types. If I have to setup all the types I have to write a lot of setup code for every test. But in a single test, I am not concerned with all of them, I simply need non-null mocked objects except for the one that I am actually testing (and for which I gladly write a more complex setup).

Wilbert
  • 7,251
  • 6
  • 51
  • 91

7 Answers7

73

In Moq 4.13 the It.IsAnyType and It.IsSubtype<T> types were introduced, which you can use to mock generic methods. E.g.:

public interface IFoo
{
    bool M1<T>();
    bool M2<T>(T arg);
}

var mock = new Mock<IFoo>();
// matches any type argument:
mock.Setup(m => m.M1<It.IsAnyType>()).Returns(true);

// matches only type arguments that are subtypes of / implement T:
mock.Setup(m => m.M1<It.IsSubtype<T>>()).Returns(true);

// use of type matchers is allowed in the argument list:
mock.Setup(m => m.M2(It.IsAny<It.IsAnyType>())).Returns(true);
mock.Setup(m => m.M2(It.IsAny<It.IsSubtype<T>>())).Returns(true);

Note that this does not work with methods that return a generic, for reasons explained here. A possible workaround is also discussed.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
John Gamble
  • 1,022
  • 1
  • 9
  • 6
  • 5
    What happened to this? Is it not available anymore? – Michael Puckett II Feb 05 '21 at 00:58
  • @MichaelPuckettII No, it still works in Moq 4.15. I think the other answer was just accepted before this existed. – Mark Wells Jul 19 '21 at 17:25
  • 14
    this does not work, if the methods you want to set up are returning T – Matus Feb 03 '22 at 14:54
  • @Matus I'm having troubles mocking a generic method having constrains on the generic parameter T (e.g.: where T: Foo). Based on my understanding, there is no way to mock the generic type argument in this scenario. Am I wrong ? – Enrico Massone May 17 '23 at 13:02
30

Unless I'm misunderstand what you need, you could build a method like this:

private Mock<IRepo> MockObject<T>()
{
    var mock = new Mock<IRepo>();
    return mock.Setup(pa => pa.Reserve<T>())
        .Returns(new Mock<IA<T>>().Object).Object;
}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • I tried this, but I keep getting 'the type or namespace 'T' could not be found' when I try to compile this. – Wilbert Nov 19 '13 at 13:43
  • 5
    Ok, the difference is of course that you put the whole thing in a generic function. I need to put it in a non-generic function. – Wilbert Nov 19 '13 at 13:45
  • @Wilbert, you can't encapsulate the code you want without making the function generic. With my edits above you can do this now: `var mock = MockObject();` and that will give you back a `Mock` that implements that type. – Mike Perrenoud Nov 19 '13 at 13:47
  • 4
    Yes, I get that, but I need the repo to support a few different types. It seems the only way to achieve this is to write lots of .Setup methods :( – Wilbert Nov 19 '13 at 13:50
  • 1
    Haha. Ah well, I just hoped there would be something in Moq that saves me the grunt work. – Wilbert Nov 19 '13 at 13:54
30

Simply do this:

[TestMethod]
public void ExampleTest()
{
  var mock = new Mock<IRepo> { DefaultValue = DefaultValue.Mock, };
  // no setups needed!

  ...
}

Since your mock does not have behavior Strict, it will be happy with calls that you haven't even set up. In that case a "default" is simply returned. Then

DefaultValue.Mock

ensures that this "default" is a new Mock<> of appropriate type, instead of just a null reference.

The limitation here is that you cannot control (e.g. make special setups on) the individual "sub-mocks" that are returned.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • This solution is ok when you don't care about null values for properties on those auto-created mock objects, since you can't influence/setup those created objects. – Mladen B. Oct 31 '19 at 09:47
9

I have found an alternative that I think gets closer to what you want. Anyway it was useful for me so here goes. The idea is to create an intermediate class which is almost purely abstract, and implements your interface. The part which is not abstract is the part Moq can't handle. E.g.

public abstract class RepoFake : IRepo
{
    public IA<T> Reserve<T>()
    {
        return (IA<T>)ReserveProxy(typeof(T));
    }

    // This will be mocked, you can call Setup with it
    public abstract object ReserveProxy(Type t);

    // TODO: add abstract implementations of any other interface members so they can be mocked
}

Now you can mock RepoFake instead of IRepo. Everything works the same except for you write your setups on ReserveProxy instead of Reserve. You can handle the Callback if you want to perform assertions based on type, though the Type parameter to ReserveProxy is totally optional.

OlduwanSteve
  • 1,263
  • 14
  • 16
1

Here's one way to do it which seems to work. If all the classes you're using in IRepo inherit from a single base class you can use this as-is and never have to update it.

public Mock<IRepo> SetupGenericReserve<TBase>() where TBase : class
{
    var mock = new Mock<IRepo>();
    var types = GetDerivedTypes<TBase>();
    var setupMethod = this.GetType().GetMethod("Setup");

    foreach (var type in types)
    {
        var genericMethod = setupMethod.MakeGenericMethod(type)
            .Invoke(null,new[] { mock });
    }

    return mock;
}

public void Setup<TDerived>(Mock<IRepo> mock) where TDerived : class
{
    // Make this return whatever you want. Can also return another mock
    mock.Setup(x => x.Reserve<TDerived>())
        .Returns(new IA<TDerived>());
}

public IEnumerable<Type> GetDerivedTypes<T>() where T : class
{
    var types = new List<Type>();
    var myType = typeof(T);

    var assemblyTypes = myType.GetTypeInfo().Assembly.GetTypes();

    var applicableTypes = assemblyTypes
        .Where(x => x.GetTypeInfo().IsClass 
                && !x.GetTypeInfo().IsAbstract 
                 && x.GetTypeInfo().IsSubclassOf(myType));

    foreach (var type in applicableTypes)
    {
        types.Add(type);
    }

    return types;
}

Otherwise, if you don't have a base class you can modify the SetupGenericReserve to not use the TBase type parameter and instead just create a list of all the types you want to set up, something like this:

public IEnumerable<Type> Alternate()
{
    return new [] 
    {
        MyClassA.GetType(),
        MyClassB.GetType()
    }
}

Note: This is written for ASP.NET Core, but should work in other versions except for the GetDerivedTypes method.

Sam
  • 4,994
  • 4
  • 30
  • 37
0

I couldn't find any information on mocking the generic methods with a generic mock method, using Moq. The only thing I found so far was mocking the generic methods per-specific-type, which is not helping, because, in general, you can't really foresee all the possible cases/variations of generic parameters in advance.

So I resolved this kind of issue by creating my own fake/empty implementation of that interface, instead of using Moq.

In your case, it would look like this:

public interface IRepo
{
    IA<T> Reserve<T>();
}

public class FakeRepo : IRepo
{
    public IA<T> Reserve<T>()
    {
        // your logic here
    }
}

Afterwards, just inject that fake implementation in place of IRepo usage.

Mladen B.
  • 2,784
  • 2
  • 23
  • 34
-1

In my case I had a generic function begin called by the tested class. Since the tested class that decides the concrete type I can't pass it on the test. I found a solution where I just call setup twice, once for each concrete type being used by the tested class, it's easier to show than to explain.

BTW I'm using Moq.AutoMock library

using Xunit;
using System;
using Moq;
using Moq.AutoMock;

namespace testexample
{
    public class Foo
    {
        // This is the generic method that I need to mock
        public virtual T Call<T>(Func<T> f) => f();
    }

    public class Bar
    {
        private readonly Foo Foo;
        public Bar(Foo foo)
        {
            Foo = foo;
        }

        public string DoSomething()
        {
            // Here I call it twice, with distinct concrete types.
            var x1 = Foo.Call<string>(() => "hello");
            var x2 = Foo.Call<int>(() => 1);
            return $"{x1} {x2}";
        }
    }

    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            var mock = new AutoMocker();

            // Here I setup Call twice, one for string
            // and one for int.
            mock.Setup<Foo, string>(x => x.Call<string>(It.IsAny<Func<string>>()))
                .Returns<Func<string>>(f => "mocked");
            mock.Setup<Foo, int>(x => x.Call<int>(It.IsAny<Func<int>>()))
                .Returns<Func<int>>(f => 2000);

            var bar = mock.CreateInstance<Bar>();

            // ... and Voila!
            Assert.Equal("mocked 2000", bar.DoSomething());
        }

        [Fact]
        public void Test2()
        {
            var bar = new Bar(new Foo());
            Assert.Equal("hello 1", bar.DoSomething());
        }
    }
}

geckos
  • 5,687
  • 1
  • 41
  • 53