gpt4 book ai didi

ruby - 如何使用 Ruby 在 RSpec 测试之间重新加载模块?

转载 作者:数据小太阳 更新时间:2023-10-29 06:54:39 24 4
gpt4 key购买 nike

我有一个可以像这样配置的模块:

module MyModule
mattr_accessor :setting
@@setting = :some_default_value
end

MyModule.setting = :custom_value

我正在使用 RSpec 测试不同的配置选项,发现设置在不同的测试之间保持不变,因为它们是类变量。

在 RSpec 测试之间重新加载和重新初始化模块的最佳方法是什么?

最佳答案

我想到了这个解决方案:

describe MyModule do

before :each do
# Removes the MyModule from object-space (the condition might not be needed):
Object.send(:remove_const, :MyModule) if Module.const_defined?(:MyModule)

# Reloads the module (require might also work):
load 'path/to/my_module.rb'
end

it "should have X value" do
MyModule.setting = :X

expect(MyModule.setting).to eq :X
end

end

来源:http://geminstallthat.wordpress.com/2008/08/11/reloading-classes-in-rspec/

关于ruby - 如何使用 Ruby 在 RSpec 测试之间重新加载模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17626123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com