为什么对依赖单例的系统进行单元测试很困难? [英] Why is it hard to unit test a system that depends on singletons?

查看:21
本文介绍了为什么对依赖单例的系统进行单元测试很困难?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读过支持和反对使用单例模式的案例.一个常见的反对案例描述了使用单例进行单元测试的困难,但我不清楚这是为什么?如果单元测试是构建的一部分,您不能只引用单例并在需要时使用它吗?(我是从 Java 的角度思考的,但我想这不重要)

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 this is? If the unit test is part of the build couldn't you just reference the singleton and use it when its needed? (I'm thinking from a java perspective but I guess it shouldnt matter)

推荐答案

关于此的一篇很棒的文章是 单身是病态的骗子.这通过一个简单的例子描述了为什么使用单例进行测试出乎意料地困难.

A great article about this is Singletons are Pathological Liars. This describes, using a simple example, why testing using singletons is unexpectedly hard.

这篇关于为什么对依赖单例的系统进行单元测试很困难?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆