Reputation: 79
I am using FakeItEasy to fake class to do the following unit test. When I debug the unit test step by step, noticed that it will not step into original method -->IsOrderHasToBeCharged(). Instead, it always default the returned value as False. Is it because the method is marked as virtual in the working class? Is there any other way to do the unit test for that method?
Upvotes: 4
Views: 1442
Reputation: 242030
When I debug the unit test step by step, noticed that it will not step into original method -->IsOrderHasToBeCharged(). Instead, it always default the returned value as False. Is it because the method is marked as virtual in the working class?
Yes. This is exactly right. When FakeItEasy creates a fake, it intercepts all calls to virtual methods and provides its own implementation, as explained at Overrideable members are faked.
If you wanted to have the original IsOrderHasToBeCharged
method to be called, you could configure the fake to call the base method. This can be done for the single method, like this:
A.CallTo(() => switchHandler.IsOrderHasToBeCharged(payCode, amount))
.CallsBaseMethod();
or for every method on the fake, at creation time:
var switchHandler = A.Fake<ChargeProcessorSwitchHandler>(
options => options.CallsBaseMethods());
Either of these will cause the original IsOrderHasToBeCharged
to be called.
However, I'm compelled to note that this is probably not the best approach for this particular test. In general, faking the system under test is a bad idea, as it can introduce confusion, similar to what you've already seen. In general, isolation frameworks ("faking frameworks") should be used to fake the types that the system under test collaborates with. Then an actual system under test is instantiated in the test and exercised.
In this case, ChargeProcessorSwitchHandler
has no collaborators, so I think you'd be better off replacing
var switchHandler = A.Fake<ChargeProcessorSwitchHandler>();
with
var switchHandler = new ChargeProcessorSwitchHandler();
Then you can leave FakeItEasy out of the test altogether.
Upvotes: 6