I have an interface
interface IService
{
IService Configure();
bool Run();
}
The "real" implementation does this:
class RealService : IService
{
public IService Configure()
{
return this;
}
public bool Run()
{
return true;
}
}
Using Moq to mock the service creates a default implementation of Configure()
that returns null
.
Is there a way to setup methods in Moq that return the mocked instance?
Assuming that you are using Moq
, you can simply Setup
the mock to return itself:
var mock = new Mock<IService>();
mock.Setup(service => service.Configure()).Returns(mock.Object);
var testClass = new TestClass(mock.Object);
Then validate if the method returned what you expect:
Assert.AreEqual(mock.Object, testClass.DoStuff());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With