簡體   English   中英

Rails + Rspec:如何測試環境特定的操作? 例如“......除非Rails.env.production?”

[英]Rails + Rspec: How to test for environment specific actions? e.g. “… unless Rails.env.production?”

我正在為支付處理器編寫一個包裝器:

class PaySimpleWrapper 
  attr_reader :transaction

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

我如何測試usessandbox屬性是否僅在非生產環境中設置?

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM