简体   繁体   English

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

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

I'm writing a wrapper for a payment processor: 我正在为支付处理器编写一个包装器:

class PaySimpleWrapper 
  attr_reader :transaction

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

How would I test that the usessandbox attribute gets set only in non-production environments? 我如何测试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.

相关问题 rspec 如果 Rails.env.production? 返回字符串怎么实现呢? - rspec if Rails.env.production? return string how to implemented it? Rails.env.production? 在登台环境中返回true - Rails.env.production? returns true in staging environment 仅在Rails.env.production中进行某些验证? - Certain validations only in Rails.env.production? rails 4 ruby​​条件基于ENV var和Rails.env.production? - rails 4 ruby conditional based on ENV var and Rails.env.production? 我怎样才能得到Rails.env.production的等价物? 在application.js中 - How can I get the equivalent of Rails.env.production? in application.js .env 没有被加载到 Rails 的测试环境中,使用 rspec - .env not being loaded in test environment in Rails with rspec Rails:测试需要访问Rails环境的帮助程序(例如request.fullpath) - Rails: test a helper that needs access to the Rails environment (e.g. request.fullpath) 我的Rails代码如何分辨它是运行在服务器(例如瘦服务器)还是rspec中? - How can my Rails code tell whether it is running in a server (e.g. thin) or in rspec? 如何配置RSPEC永远不会在RAILS_ENV生产上运行 - How to configure RSPEC to never run on RAILS_ENV production 如何使用Rails中的minitest测试我的控制器中的(例如)@project.save的失败? - How do I test for failure of (e.g.) @project.save in my controller using minitest in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM