4 ms·
Yes, you gain testability. If your global state contains something that runs in prod but should not run in a testing environment (e.g. a database connection),
by billmcneale 2y ago
Yes, you gain testability.
If your global state contains something that runs in prod but should not run in a testing environment (e.g. a database connection), your global variable based code is now untestable.
Dependency Injection is popular for a very good reason.
- caspper69 2y agoThis sounds like a design deficiency. If you have something that should only run in testing, perhaps your test harness should set the global variable appropriately, no?
- stickfigure 2y agoSure. And a million programmers have all screamed out in horror when they realize that their single test passes, but fails when run as part of the whole suite. Test contamination is a hell paved with global variables.
- Tainnor 2y agoI can't agree more, I was dying this death a thousand times over at my last job.