我在应用程序配置中定义了一个选项。我要测试的课程是在 gem 中定义的(不是我写的)。我想重新开课:
Myclass.class_eval do
if Rails.application.config.myoption=='value1'
# some code
def self.method1
end
else
# another code
def self.method2
end
end
end
我想使用 RSpec 3 测试这段代码:
# myclass_spec.rb
require "rails_helper"
RSpec.describe "My class" do
allow(Rails.application.config).to receive(:myoption).and_return('value1')
context 'in taxon' do
it 'something' do
expect(Myclass).to respond_to(:method1)
end
end
end
如何在运行重新打开类的代码之前存根应用程序配置值。