How can you use Lazy<> with AutoMock (Moq)

Viewed 1908

We are using Autofac.Extras.Moq.AutoMock. Now I have a constructor dependency using Lazy<>

public MyService(Lazy<IDependency> myLazyDependency) {...}

to test MyService we need to mock the Lazy<Dependency>.

I am trying this with

[ClassInitialize]
public static void Init(TestContext context)
{
    autoMock = AutoMock.GetLoose();
}

[TestInitialize]
public void MyTestInitialize()
{
     var myDepMock = autoMock.Mock<Lazy<IDependency>>();  // <-- throws exception
}

This is the exception returned by the test runner:

Initialization method Tests.MyServiceTests.MyTestInitialize threw exception. System.InvalidCastException: System.InvalidCastException: Unable to cast object of type 'System.Lazy1[IDependency]' to type 'Moq.IMocked1[System.Lazy`1[IDependency]]'..

So, how can I pass a Lazy<> mocked object using automock.

1 Answers
Related