As the exact real code is a bit hard to explain and it involves some specific domain knowledge I'll try to hide it as much as possible.
The module that I'm trying to test uses a common context that is shared across the application. This context maintains a list that it's filtered (in this case) more or less like this:
var specialItems = SharedContext.GetSpecialItemsBySomeRules();
foreach item in specialItems {
//AddItemAndSpecialItemsToSharedContext virtual function
if (somethingHappens)
_someService.AddItemAndSpecialItemsToSharedContext(parameters);
}
It's a bit weird, but the important thing is that items that exist in the SharedContext determine how many will be added when executing this function.
I was trying to test how many times the function AddItemAndSpecialItemsToSharedContext was called in a specific scenario where it should be called only once. I'm using C# and RhinoMocks.
What is happening now is that, as someService is a mock and the return type of the function is a reference, it is returning null and no code is being executed. As no code is being executed by AddItemAndSpecialItemsToSharedContext, the collection inside SharedContext is not being modified and tests are failing (because it causes the function to be called twice).
SomeService is quite complex with some dependencies, and If I need to create my own fake, I will be forced to create more stubs, and the test will grow too much. It would be great to be able to override only some part of the behavior and maintain only the part of the logic that adds elements to SharedContext so the tests passes.
Also, I'm very interested in knowing what it's the best way (according to best practices) to test this kind of scenario. I initially thought that I only needed to test the interaction with SomeService (number of times called) but now I understand that I need it to execute some of its logic (which I don't know if it's a smell of some sort. Is something wrong here?).
Some code involved, is legacy code. Anyway, any opinion or advice will be greatly appreciated, even if I can't apply the solution.
Aucun commentaire:
Enregistrer un commentaire