If I have a class that has a dependency that is resolved via property injection, is it possible to Mock the behavior of that property using Moq?
e.g.
public class SomeClass
{
//empty constructor
public SomeClass() {}
//dependency
public IUsefuleService Service {get;set;}
public bool IsThisPossible(Object someObject)
{
//do some stuff
//I want to mock Service and the result of GetSomethingGood
var result = Service.GetSomethingGood(someObject);
}
}
So, SomeClass is under test and I am trying to figure out if I can mock the behavior of IUsefulService with Moq so when I test IsThisPossible and the line using the service is hit, the mock is used…
I may be misunderstanding and oversimplifying the question, but I think code below should work. Since you have the
Serviceproperty as a public property, you can just mockIUsefulService, new upSomeClass, and then set theServiceproperty onSomeClassto your mock.Hope that helps. If I’m missing something let me know and I’ll see what I can do.