Is there a way to specify a return object for every method and property of specific type?
For example I want all method, that returns string, return "TestString" instead of null.
Usually, I have to write something like this:
public interface ISomeInterface
{
string GetValue();
}
public void Test()
{
var mock1 = Substitute.For<ISomeInterface>();
mock1.GetValue().Returns("TestString");
var mock2 = Substitute.For<ISomeInterface>();
mock2.GetValue().Returns("TestString");
// ... test something
}
But is there a way to specify that all created ISomeInterface substutes will return "TestString" for GetValue() method?
PS. There is an issue at NSubstitute github, where I've asked the same question with more specific details. https://github.com/nsubstitute/NSubstitute/issues/751
The answer is written here - https://github.com/nsubstitute/NSubstitute/issues/751.
Long story short, you have to implement
IAutoValueProviderandIAutoValueProviderFactoryfor your type. For this specific case it could look like this: