I'm having a really hard time trying to figure how I can do .SetupXXX()
calls on the underlying Mock<T>
that has been generated inside the MockingKernel
. Anyone who can shed some light on how it is supposed to work?
You need to call the GetMock<T>
method on the MoqMockingKernel
which will return the generated Mock<T>
on which you can call your .SetupXXX()/VerifyXXX()
methods.
Here is an example unit test which demonstrates the GetMock<T>
usage:
[Test]
public void Test()
{
var mockingKernel = new MoqMockingKernel();
var serviceMock = mockingKernel.GetMock<IService>();
serviceMock.Setup(m => m.GetGreetings()).Returns("World");
var sut = mockingKernel.Get<MyClass>();
Assert.AreEqual("Hello World", sut.SayHello());
}
Where the involved types are the following:
public interface IService { string GetGreetings(); }
public class MyClass
{
private readonly IService service;
public MyClass(IService service) { this.service = service; }
public string SayHello()
{
return string.Format("Hello {0}", service.GetGreetings());
}
}
Note that you can access the generated Moq.MockRepository
(if you prefer it over the SetupXXX methods) with the MoqMockingKernel.MockRepository
property.