如何测试单例类? [英] How to test a Singleton class?

查看:35
本文介绍了如何测试单例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RSpec 并且想多次测试 Singleton 类的构造函数.

I am using RSpec and want to test the constructor of a Singleton class more than one time.

我该怎么做?

最好的问候

推荐答案

看看 http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby:

require 'singleton'

class <<Singleton
  def included_with_reset(klass)
    included_without_reset(klass)
    class <<klass
      def reset_instance
        Singleton.send :__init__, self
        self
      end
    end
  end
  alias_method :included_without_reset, :included
  alias_method :included, :included_with_reset
end

这篇关于如何测试单例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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