I have this simple example where I would like to test if a method is being invoked on the same class as the calling method:
public class MyClass
{
public void SomeMethod()
{
SomeSubMethod();
}
public virtual void SomeSubMethod()
{
// do a lot of weird stuff
}
}
public class UnitTest1
{
[Fact]
public void Test1()
{
var target = Substitute.ForPartsOf<MyClass>();
target.Configure().SomeSubMethod(); // <---- please just do nothing
target.SomeMethod();
target.Received(1).SomeSubMethod();
}
}
My problem is that SomeSubMethod
is actually invoked in the unit test, and in my real code I would like to avoid that.
A simple work-a-round is to let SomeSubMethod
return something, but now I'm polluting my real code
public class MyClass
{
public void SomeMethod()
{
SomeSubMethod();
}
public virtual int SomeSubMethod()
{
// do a lot of weird stuff
return 0;
}
}
public class UnitTest1
{
[Fact]
public void Test1()
{
var target = Substitute.ForPartsOf<MyClass>();
target.Configure().SomeSubMethod().Returns(0); // <--- Now the real SomeSubMethod won't be invoked
target.SomeMethod();
target.Received(1).SomeSubMethod();
}
}
Is there a way to configure a void method to do nothing?
Yours /peter