vendredi 28 avril 2023

Injecting and mocking helper classes vs. actual functionality?

I have a helper class FooItem that acts as a facade for another class Foo. I'm now creating another class Bar which internally uses FooItem to do its Foo related actions.

Since FooItem is a facade, my initial thought is to neither inject it in Bar, nor mock during testing, and instead, when testing Bar, assert against Foo directly. The idea is that FooItem is inconsequential to what Bar should be doing with Foo.

Is this approach better than injecting a FooItem into Bar and also asserting on its methods instead of Foo? It's tested itself, so technically asserting against it should be safe.

Some thoughts:

  • If asserting on FooItem in Bar, if it breaks in the future, Bar's tests will fail as collateral.
  • Asserting on a facade or helper feels intuitively wrong since it feels most like an internal implementation detail that should be inconsequential to the actual goal target, which is communicating with Foo.
  • In the future, if I use another utility instead of FooItem or directly act on Foo instead, the tests would be remain the same if I dont assert on FooItem.

This might actually be two separate discussions, one about injecting FooItem into Bar when instantiating it and another about asserting against Foo or FooItem in Bar's tests.

Aucun commentaire:

Enregistrer un commentaire