Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why consider_all_requests_local fails with rspec config

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

Code:

class ApplicationController < ActionController::Base
  unless Rails.application.config.consider_all_requests_local
    rescue_from ActiveRecord::RecordNotFound, :with => :render_404
    rescue_from Exception, :with => :render_500
    rescue_from FunnyException, :with => :my_errors

    def my_errors(exception)
      #some stuff for production
      puts "=======From top====#{Rails.application.config.consider_all_requests_local}"
    end
  else
    rescue_from FunnyException, :with => :my_errors
    def my_errors(exception)
      #some stuff for development
      puts "=====From bottom======#{Rails.application.config.consider_all_requests_local}"
    end
  end
end

This code perfectly works with production/development... problem is when I wanna test it with rspec. How to switch test case between environments?

I tried adding something like:

before do
  Rails.application.config.consider_all_requests_local = true
end

I get:

...=====From bottom======true .=====From bottom======true .=====From bottom======true .=====From bottom======true

so tried

before do
  Rails.application.config.consider_all_requests_local = false
end

so I get:

...=====From bottom======false .=====From bottom======false .=====From bottom======false .=====From bottom======false

How is that even possible? ... same with changing spec_helper

config.before(:each) do
  Rails.application.config.consider_all_requests_local = false
end

unless is not working. Top section is unreachable with rspec... always hits Bottom why ? I assume it is somehow tied now with Rack... but is there any solution to dynamically change this behavior inside rspec?

regards

like image 348
Piotr Mąsior Avatar asked Jan 25 '12 18:01

Piotr Mąsior


3 Answers

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

like image 132
TerryYin Avatar answered Nov 11 '22 10:11

TerryYin


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
like image 28
maikel Avatar answered Nov 11 '22 08:11

maikel


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

like image 22
Piotr Mąsior Avatar answered Nov 11 '22 10:11

Piotr Mąsior