Why is it hard to unit test a system that depends on singletons?

前端 未结 7 1890
Happy的楠姐
Happy的楠姐 2020-12-02 23:41

I\'ve read cases for and against using the singleton pattern. One common case against describes the difficulties in unit testing with singletons, but I\'m unclear as to why

7条回答
  •  悲哀的现实
    2020-12-02 23:59

    Because the singleton is an OOPish global variable. Basically, all functions relying on the use of the singleton (directly or indirectly) are not guaranteed to be deterministic (i.e. you cannot expect the function to return the same outputs for the same inputs T each and every run).

提交回复
热议问题