This scenario unexpectedly throws "shouldn't be here" exception which means methods are not mocked out. As soon as you change method access from protected to public, all works as expected. It seems that only public methods get mocked out. Is this by design? I would expect all methods to be mocked out unless I specify Behavior.CallOriginal.
Here's simplified setup that also fails for the test above:
public class Test1
{
protected void Testing1()
{
string s = "Testing1";
throw new Exception("shouldn't be here");
}
}
public class Test2 : Test1
{
protected void Testing2()
{
Testing1();
string s = "Testing2";
throw new Exception("shouldn't be here");
}
}
public class Test3 : Test2
{
protected void Testing3()
{
Testing1();
Testing2();
string s = "Testing3";
}
}
[Test]
public void MakeSureAllMethodsAreMockedOut()
{
Test3 test3 = Mock.Create<
Test3
>();
Mock.NonPublic.Arrange(test3, "Testing3").CallOriginal().MustBeCalled();
test3.GetType().GetMethod("Testing3", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).Invoke(test3, new object[] { });
}
public class Test3
{
protected void Testing1()
{
string s = "Testing1";
throw new Exception("shouldn't be here");
}
public void Testing3()
{
Testing1();
string s = "Testing3";
}
}