There are a few problems with your approach.
The first problem is that you've no connection between the fakeBaseClass
you created and derivedClass
, so there's no way for them to influence one another. To accomplish what I think you are trying to do, you really want to make a fake DerivedClass
and manipulate its behaviour (actually, in general, this type of testing is contentious, but let's focus on the mechanics and set aside whether it's a good idea for now).
By faking DerivedClass
, FakeItEasy will actually be instantiating a new type of class that derives from DerivedClass
and therefore will call and have access to all (protected or public) methods of DerivedClass
and BaseClass
. Then you can set up the behaviour you want from BaseClassMethod
and exercise DoSomething
and hopefully get the results you want.
The second thing that will cause problems when we try this is that BaseClassMethod
is not virtual. FakeItEasy (and isolation frameworks of its ilk) cannot fake out any method that is not virtual. You can learn more about what's fakeable at What can be faked.
So, putting together those two changes, I get code like this:
public class BaseClass
{
protected virtual bool BaseClassMethod()
{
return true;
}
}
public class DerivedClass : BaseClass
{
public string DoSomething()
{
if (BaseClassMethod())
{
return ("Bla");
}
else
{
return ("Worked");
}
}
}
[Test]
public void MethodName_StateUnderTest_ExpectedBehavior()
{
var fakedDerivedClass = A.Fake<DerivedClass>();
A.CallTo(fakedDerivedClass)
.Where(x => x.Method.Name == "BaseClassMethod")
.WithReturnType<bool>()
.Returns(false);
Assert.That(fakedDerivedClass.DoSomething(), Is.EqualTo("Worked"));
}
This test passes. Note that I've made changes to your original (aside from correcting typos):
BaseClassMethod
is now virtual
- I'm creating a fake
DerivedClass
for testing, and
DoSomething
returns a string, rather than writing to the Console. That wasn't strictly necessary, but made my test easier to write.
Note that if BaseClassMethod
is not virtual, then there's no chance it will return anything other than true
, and I don't see much point in writing a test to see what will happen when DoSomething
gets a false
from it.
For more discussion on why faking the class under test (instead of that class's collaborators) may be a bad idea, see comments on Use FakeItEasy's A.CallTo() on another method in same object and my answer to Using FakeItEasy, is it possible to create a dummy object of a type that takes generic type parameters.
But how you feel about the approach is something that you will have to come to terms with on your own.