Why consider_all_requests_local fails with rspec config

后端 未结 4 1561
名媛妹妹
名媛妹妹 2021-01-04 12:00

rspec-rails (2.7.0) rails (3.0.10) post: Rails 3.1 Error Catching is irrelevant for me.

Code:

class ApplicationController < ActionController::Base         


        
相关标签:
4条回答
  • 2021-01-04 12:13

    The logic block is defined on class level, which gets evaluated during the class load time. This is why it will not go to 'else' even if you manually set it to false at run time.

    I am also curious to know what's the best way to test this. My only clue is somehow reload or re eval ActionController, similar to reload! method in rails console.

    0 讨论(0)
  • 2021-01-04 12:15

    You also need to set Rails.application.config.action_dispatch.show_exceptions = true. But since Rails caches that value, this will only work if you run your spec on its own. Fortunately, you can change these options only for the current spec by mocking the config:

      before do
        method = Rails.application.method(:env_config)
        expect(Rails.application).to receive(:env_config).with(no_args) do
          method.call.merge(
            "action_dispatch.show_exceptions" => true,
            "action_dispatch.show_detailed_exceptions" => false,
            "consider_all_requests_local" => false
          )
        end
      end
    

    Thanks to:

    • http://atodorov.org/blog/2016/04/27/changing-rails-consider_all_requests_local-in-rspec-fails/
    • https://thepugautomatic.com/2014/08/404-with-rails-4/#comment-1714338343
    0 讨论(0)
  • 2021-01-04 12:23

    I found that with cucumber-rails the right way of doing it is to add the @allow-rescue tag.

    0 讨论(0)
  • 2021-01-04 12:30

    Ok I found quite easy solution

    before do
      Rails.application.config.consider_all_requests_local = false
      load "application_controller.rb"
    end
    
    after do
      Rails.application.config.consider_all_requests_local = true
      load "application_controller.rb"
    end
    

    It is part of anonymous application controller test suite.

    You have to add after block... because this change will persist through other suites.

    Any improvements welcome :D

    edit: Using spork and guard causes for me sometimes random errors... before :all seems to solve that problem

    0 讨论(0)
提交回复
热议问题