dimanche 26 juin 2016

Unit testing functions - passing dependencies as args vs global access

Consider a scenario where a function A depends on three functions — P, Q and R. P, Q, R themselves have been unit tested as they do some really complex computations. Now I need to test the function A and I have two options —

Access P, Q, R directly from A

function A (params) {
   ... P() 
   ... Q() 
   ... R() 
}

PROs: No mocking required except for params.
CONs: Unnecessarily testing the logic of P, Q & R.

Inject P, Q, R as arguments into A

function A (P, Q, R, params) {
   ... P()
   ... Q()
   ... R()
}

PROs: A is tested in a more controlled environment, as P, Q, R as passed as args.
CONs: A lot of effort goes in keeping the mocked functions update to date with their original functions.

I want to know which approach is better of the two and how can I control their respective cons in a better fashion.

NOTE: A, P, Q, R are all pure.

Aucun commentaire:

Enregistrer un commentaire