What's the best practice for testing public methods that use private variable? -


so have class 2 public methods init() , dostuff(). init() sets private variable $_myprivvar. dostuff() method uses variable.

when writing unit tests dostuff() what's best practice setting $_myprivvar? presumably don't want use init() unit test testing 2 units of code?

this contrived version of real world scenario.

first unit in unit test not 1 method - can class or whatever well.

but more importantly: if have function depends on 1 argument (your $_myprivvar) please implement function takes value input.

if make dependencies inputs (aka making pure function) see 1 testable.

you can wrap later if want - 1 argument ... don't bother


the other way provide public setter variable - languages have compiler variables/symbol/whatever call it (for example c# , #if ...) can use remove setters release version later.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -