Routing rspec with nested routes

Problem

You would like to add a routing rspec test in your rails application that tests for a nested route.
So while you have something like the following in your config/routes.rb file:

namespace :admin do
  resources :users do
    resources :audits, only: [:index]
  end
end

you want to add the following test for your route:

expect(get: '/admin/users/:user_id/audits').to route_to(controller: 'admin/audits', action: 'index')

but you are getting an error like the following:

The recognized options <{"controller"=>"admin/audits", "action"=>"index", "user_id"=>":user_id"}> did not match <{"controller"=>"admin/audits", "action"=>"index"}>, difference:.
       --- expected
       +++ actual
       @@ -1 +1 @@
       -{"controller"=>"admin/audits", "action"=>"index"}
       +{"controller"=>"admin/audits", "action"=>"index", "user_id"=>":user_id"}

Solution

You will need to change your rspec to the following using any number for the user_id:

expect(get: '/admin/users/42/audits').to route_to(controller: 'admin/audits', action: 'index', user_id: '42')

Solution adapted from here

make the unpermitted parameters errors in Ruby on Rails more obvious

Problem

When you are adding new attributes to your model after the initial creation, they are not by default included in the permitted parameters list. Sometimes the error is not so obvious to see when testing, unless you look at the test log files.

Solution

To make the error more obvious, there is the following configuration option that you can set up in config/environments/test.rb (tip taken from Rails 4 In Action):

config.action_controller.action_on_unpermitted_parameters = :raise

Cucumber testing and ssl

Problem

Your application is ussing force_ssl to redirect all calls to ‘https’, but when you try to use your cucumber tests they fail.

Solution

By using the suggestion here, you can add a file in your initializers to bypass the ssl in development and test environment as follows:

module ActionController
  module ForceSSL
    module ClassMethods
      def force_ssl(options = {})
        before_filter(options) do
          if !request.ssl? && !Rails.env.development? && !Rails.env.test?
            redirect_to :protocol => 'https://', :status => :moved_permanently
          end
        end
      end
    end
  end
end

Could not open library ‘libgtkmm-2.4.so’: libgtkmm-2.4.so: cannot open shared object file: No such file or directory.

Problem

If you have setup guard with your Rails project and you are trying to run it in an (K)Ubuntu installation you get the following message:

Could not open library 'libgtkmm-2.4.so': libgtkmm-2.4.so: cannot open shared object file: No such file or directory.

Solution

Install the missing library with the following:

sudo apt-get install libgtkmm-2.4

assert_difference and Rails 2.1.0

Problem
Trying to use the assert_difference with Rails 2.1.0 produces errors if trying to use it with the syntax of another rails version.

Solution
The correct syntax for using assert_difference with rails 2.1.0 is:

def test_should_create_user

assert_difference(User, :count, 1) do

user = create_user

assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"

end

end