Rails + Rspec:如何测试环境特定的操作?例如" ...除非Rails.env.production?"

时间:2014-06-06 19:35:57

标签: ruby-on-rails

我正在为付款处理器编写包装器:

class PaySimpleWrapper 
  attr_reader :transaction

  def initialize(args={})
    @transaction.usesandbox = true unless Rails.env.production?
  ....

我如何测试usessandbox属性是否仅在非生产环境中设置?

1 个答案:

答案 0 :(得分:3)

  describe PaySimpleWrapper do
    let(:wrapper) { PaySimpleWrapper.new }

    describe "when in production" do
      before { Rails.stub_chain(:env, :production?) { true } }
      it "uses the live system" do
        expect(wrapper.transaction.usesandbox).to be_false
      end
    end

    describe "when not in production" do
      before { Rails.stub_chain(:env, :production?) { false } }
      it "uses the sandbox" do
        expect(wrapper.transaction.usesandbox).to be_true
      end
    end
  end