Leave a comment

Ruby on Rails testing with Select2


I posted a while back about using the select2-rails gem together with the select2-capistrano gem. I recently upgraded an app and found that my tests that were using select2 were all failing. I did some googling and it seems that the select2-capistrano gem is no longer active.

Well, I went about it another way then.

First off, I do not add the class to each drop-down select but rather I do it globally using a collection_select_input.rb file in app/inputs/ like so:

    class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
      def input_html_classes
        super.push('chosen-select')
      end
    end

So when I started to have issues with capybara not being able to use the select2 drop-down my fix was to only use the global push if not in test environment:

    class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
      unless Rails.env.test?
        def input_html_classes
          super.push('chosen-select')
        end
      end
    end

So now I have good old normal drop downs in test environment and the ever awesome select2 drop downs in production.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: