RSpec存根环境助手

时间:2016-01-28 15:16:10

标签: rspec rspec-rails stub

我有一个包含帮助方法的lib / require / environment_helpers.rb文件。有更多方法,但它没有包含在模块中。

def development?
  Rails.env.development?
end

def test?
  Rails.env.test?
end

我试图将这些方法排除在外并且没有任何运气。我试过了:

before :each
  allow(Rails.env).to receive(:production?) { false }
end

before :each
  allow(Rails).to receive(:production?) { false }
end

before :each
  allow(ServiceUnderTest).to receive(:production?) { false }
end

1 个答案:

答案 0 :(得分:0)

  

尝试存根这些方法

你可以像这样存根

before :each do
  rails_env = double(:rails_env, development?: false)
  expect(Rails).to receive(:env).and_return(rails_env)
  expect(rails_env).to receive(:development?).and_return(false)
end