如何在Ruby中重置单个对象?我知道人们永远不想在真正的代码中执行此操作但是单元测试呢?
这是我在RSpec测试中尝试做的事情 -
describe MySingleton, "#not_initialised" do
it "raises an exception" do
expect {MySingleton.get_something}.to raise_error(RuntimeError)
end
end
失败是因为我以前的一个测试初始化了单例对象。我试过跟随Ian White的this链接的建议,其中基本上是猴子补丁Singleton提供reset_instance方法,但我得到一个未定义的方法'reset_instance'异常。
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
describe MySingleton, "#not_initialised" do
it "raises an exception" do
MySingleton.reset_instance
expect {MySingleton.get_something}.to raise_error(RuntimeError)
end
end
在Ruby中最常用的方法是什么?
答案 0 :(得分:27)
棘手的问题,单身人士很粗暴。部分是因为你正在展示(如何重置它),部分是因为他们做出的假设有时候会让你感到困惑(例如大部分的Rails)。
你可以做几件事,他们最好都“好”。最好的解决方案是找到摆脱单身人士的方法。我知道,这是手工波浪,因为没有可以应用的公式或算法,它会消除很多便利,但如果你能做到,那通常是值得的。
如果你不能这样做,至少尝试注入单例而不是直接访问它。现在测试可能很难,但想象一下在运行时必须处理这样的问题。为此,您需要内置基础架构来处理它。
以下是我想到的六种方法。
提供类的实例,但允许实例化类。这与单身传统的呈现方式最为一致。基本上,只要您想要引用单例,就可以与单例实例进行对话,但是您可以针对其他实例进行测试。 stdlib中有一个模块可以帮助解决此问题,但它会使.new
变为私有模块,因此如果您想使用它,则必须使用类似let(:config) { Configuration.send :new }
的内容来测试它。< / p>
class Configuration
def self.instance
@instance ||= new
end
attr_writer :credentials_file
def credentials_file
@credentials_file || raise("credentials file not set")
end
end
describe Config do
let(:config) { Configuration.new }
specify '.instance always refers to the same instance' do
Configuration.instance.should be_a_kind_of Configuration
Configuration.instance.should equal Configuration.instance
end
describe 'credentials_file' do
specify 'it can be set/reset' do
config.credentials_file = 'abc'
config.credentials_file.should == 'abc'
config.credentials_file = 'def'
config.credentials_file.should == 'def'
end
specify 'raises an error if accessed before being initialized' do
expect { config.credentials_file }.to raise_error 'credentials file not set'
end
end
end
然后,您想要访问它的任何地方,请使用Configuration.instance
使单例成为其他类的实例。然后你可以单独测试另一个类,而不需要明确地测试你的单例。
class Counter
attr_accessor :count
def initialize
@count = 0
end
def count!
@count += 1
end
end
describe Counter do
let(:counter) { Counter.new }
it 'starts at zero' do
counter.count.should be_zero
end
it 'increments when counted' do
counter.count!
counter.count.should == 1
end
end
然后在你的应用程序的某个地方:
MyCounter = Counter.new
您可以确保永远不会编辑主类,然后只需将其子类化进行测试:
class Configuration
class << self
attr_writer :credentials_file
end
def self.credentials_file
@credentials_file || raise("credentials file not set")
end
end
describe Config do
let(:config) { Class.new Configuration }
describe 'credentials_file' do
specify 'it can be set/reset' do
config.credentials_file = 'abc'
config.credentials_file.should == 'abc'
config.credentials_file = 'def'
config.credentials_file.should == 'def'
end
specify 'raises an error if accessed before being initialized' do
expect { config.credentials_file }.to raise_error 'credentials file not set'
end
end
end
然后在你的应用程序的某个地方:
MyConfig = Class.new Configuration
确保有办法重置单身。或者更一般地说,撤消你做的任何事情。 (例如,如果你可以用单例注册一些对象,那么你需要能够在Rails中取消注册它,例如,当你继承Railtie
时,它会在数组中记录它,但是你可以{{3 }})。
class Configuration
def self.reset
@credentials_file = nil
end
class << self
attr_writer :credentials_file
end
def self.credentials_file
@credentials_file || raise("credentials file not set")
end
end
RSpec.configure do |config|
config.before { Configuration.reset }
end
describe Config do
describe 'credentials_file' do
specify 'it can be set/reset' do
Configuration.credentials_file = 'abc'
Configuration.credentials_file.should == 'abc'
Configuration.credentials_file = 'def'
Configuration.credentials_file.should == 'def'
end
specify 'raises an error if accessed before being initialized' do
expect { Configuration.credentials_file }.to raise_error 'credentials file not set'
end
end
end
克隆课程,而不是直接测试。这来自我制作的access the array and delete the item from it,基本上你编辑克隆而不是真正的类。
class Configuration
class << self
attr_writer :credentials_file
end
def self.credentials_file
@credentials_file || raise("credentials file not set")
end
end
describe Config do
let(:configuration) { Configuration.clone }
describe 'credentials_file' do
specify 'it can be set/reset' do
configuration.credentials_file = 'abc'
configuration.credentials_file.should == 'abc'
configuration.credentials_file = 'def'
configuration.credentials_file.should == 'def'
end
specify 'raises an error if accessed before being initialized' do
expect { configuration.credentials_file }.to raise_error 'credentials file not set'
end
end
end
在模块中开发行为,然后将其扩展到单例。 gist是一个稍微复杂的例子。如果你需要在对象上初始化一些变量,你可能需要查看Here和self.included
方法。
module ConfigurationBehaviour
attr_writer :credentials_file
def credentials_file
@credentials_file || raise("credentials file not set")
end
end
describe Config do
let(:configuration) { Class.new { extend ConfigurationBehaviour } }
describe 'credentials_file' do
specify 'it can be set/reset' do
configuration.credentials_file = 'abc'
configuration.credentials_file.should == 'abc'
configuration.credentials_file = 'def'
configuration.credentials_file.should == 'def'
end
specify 'raises an error if accessed before being initialized' do
expect { configuration.credentials_file }.to raise_error 'credentials file not set'
end
end
end
然后在你的应用程序的某个地方:
class Configuration
extend ConfigurationBehaviour
end
答案 1 :(得分:23)
我想这样做会解决你的问题:
describe MySingleton, "#not_initialised" do
it "raises an exception" do
Singleton.__init__(MySingleton)
expect {MySingleton.get_something}.to raise_error(RuntimeError)
end
end
甚至更好地在回调之前添加:
describe MySingleton, "#not_initialised" do
before(:each) { Singleton.__init__(MySingleton) }
end
答案 2 :(得分:1)
提取一个TL; DR来自上面的更长的答案,对于像我这样的未来懒人 - 我觉得这很简单:
如果你之前有这个
let(:thing) { MyClass.instance }
改为
let(:thing) { MyClass.clone.instance }