My tests use Moq and AutoFixture, and often they are very verbose because they have many mock Setup() calls to configure mocks to return values created by AutoFixture. To make the tests easier to read and maintain I am trying to use AutoMoqCustomization with the ConfigureMembers feature to avoid unnecessary Setup() calls.
Mostly this is working as expected, however I have some interfaces with generic functions (AutoMapper mostly) that the AutoMoqCustomization does not seem to handle. Instead of returning an instance of the type from AutoFixture I receive a mocked instance.
I can achieve the behavior that I want by including a Setup() call for the generic function, but my goal was to remove as many of these Setup calls as possible.
I have set up the example below to reproduce the problem (in practice I am using AutoFixture to inject an IMapper instance via the constructor of another object, and that object makes the call to the IMapper interface, but that is not necessary to see the problematic behavior).
What I expect is for the call to sut.Map<object>() to work like the call to sut.Map(), returning the instance of object frozen in the fixture. Instead in variable retB I see an instance of ObjectProxy.
Including the commented line in the example that sets up the return value for Map<object>() will cause the test to pass, but I would prefer to omit this call in the same way that I can omit the Setup() call for Map().
Are generic functions supposed to auto-configured? Am I setting it up incorrectly?
using AutoFixture;
using AutoFixture.AutoMoq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UnitTestProject1
{
public interface IMapper
{
object Map();
object Map<T>();
}
[TestClass]
public class Tests
{
[TestMethod]
public void Test()
{
var f = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true });
var model = f.Freeze<object>();
var sut = f.Create<IMapper>();
//Mock.Get(sut).Setup(x => x.Map<object>()).ReturnsUsingFixture(f);
var retA = sut.Map();
var retB = sut.Map<object>();
Assert.AreEqual(model, retA);
Assert.AreEqual(model, retB); }
}
}