change rails environment in the mid of testing

You could do Rails.stub(env: ActiveSupport::StringInquirer.new(“production”)) Then Rails.env, Rails.development? etc will work as expected. With RSpec 3 or later you may want to use the new “zero monkeypatching” syntax (as mentioned by @AnkitG in another answer) to avoid deprecation warnings: allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new(“production”)) I usually define a stub_env method in a spec helper so I don’t have … Read more

Capybara tests with :js=>true… Routing Error: No route matches [GET] “/assets”

Your missing route looks like some auto-generated assets path with nil input. Are you sure your assets are correctly specified in your css/sass or wherever this route comes from? I have been struggling with a similiar issue where an asset path induced a routing error 1) user signup: [ JS ] : creates User on … Read more

Same Model for Two belongs_to Associations

Thanks to jamesw over at RailsForum.com: Same Model for Two belongs_to Associations a solution has been found. class System < ActiveRecord::Base belongs_to :project_manager, :class_name => ‘PointOfContact’, :foreign_key => ‘project_manager_id’ belongs_to :technical_manager, :class_name => ‘PointOfContact’, :foreign_key => ‘technical_manager_id’ end class PointOfContact < ActiveRecord::Base has_many :project_managed_systems, :class_name => ‘System’, :foreign_key => ‘project_manager_id’ has_many :technical_managed_systems, :class_name => ‘System’, … Read more

How can I test :inclusion validation in Rails using RSpec

Use shoulda_matchers In recent versions of shoulda-matchers (at least as of v2.7.0), you can do: expect(subject).to validate_inclusion_of(:active).in_array(%w[Y N]) This tests that the array of acceptable values in the validation exactly matches this spec. In earlier versions, >= v1.4 , shoulda_matchers supports this syntax: it {should ensure_inclusion_of(:active).in_array(%w[Y N]) }

Capybara does not pass header after form submit

This feature was added to Capybara on April 25th, 2011 with this commit – https://github.com/jnicklas/capybara/commit/a00435d63085ac2e74a0f64e7b7dedc0f7252ea9 You can can now specify a custom header when using a custom Capybara driver. See http://automagical.posterous.com/creating-a-custom-capybara-driver for code examples.