简体   繁体   English

服务定位器模式下的单元测试

[英]Unit testing in case of a service locator pattern

I am using moles framework to unit test my code. 我正在使用moles框架对我的代码进行单元测试。

There is an implementation of a singleton class with 有一个单例类的实现

  1. A private (empty) constructor 私有(空)构造函数
  2. A public static readonly Instance variable 公共静态只读实例变量

This class implements an abstract class. 此类实现一个抽象类。

How do I circumvent the Initialization function of my singleton class? 如何规避我的单例类的初始化函数? This Initialization function is called from the constructor of the abstract base class. 从抽象基类的构造函数中调用此初始化函数。

You can subclass your singleton to make a "for testing" version. 您可以将单例子类化以制作“用于测试”的版本。 You can make your initilization function abstract, and have your real singleton implement what you're currently doing, and have the testing version implement something to initialize your test version. 您可以使初始化函数抽象,让真正的单例实现您当前正在做的事情,让测试版本实现一些东西来初始化您的测试版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM