如何在rspec中测试使用Settings.production确定的环境?

时间:2016-01-28 23:55:18

标签: ruby rspec sinatra

我在Sinatra项目中有逻辑,根据环境是生产还是开发来确定不同的行为。

if Services.production?
    # do something
else
    # do something else
end

如何测试此代码?我尝试了以下但是没有用:

expect_any_instance_of(Services).to receive(:production?).and_return(true)

2 个答案:

答案 0 :(得分:1)

它不是Services您正在呼叫production?的实例,它是Services类本身。你应该能够做到

expect(Services).to receive(:production?).and_return(true)

答案 1 :(得分:1)

从您的代码看起来像生产?是一个类方法,因此它不是在服务实例上调用,而是在类Services上调用。 尝试

expect(Services).to receive(:production?).and_return(true)