Search code examples
ruby-on-railscucumberwebrat

Cucumber and/or Webrat hates  ?


I have a cucumber step that recently started failing when an   was added to my layout. If I take the   out, my tests all pass. When I put it back in, every test that uses the click_link method supplied by WebRat fails with the following message:

And he follows 'Unsubscribe'
  incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string) (Encoding::CompatibilityError)
  (eval):3:in `click_link`
  (eval):2:in `click_link`
  /path_to_project/webrat_steps.rb:19:in `/^(I|he|she) follows? '([^\"]*)'$/'
  features/manage_subscriptions.feature:59:in `And he follows 'Unsubscribe''

Does anyone have any suggestions?


Solution

  • I had the same problem under Ruby 1.9 and Rails 2.3.2, in order to get it working I had to make the following changes in the webrat gem.

    In lib/webrat/core/locators/link_locator.rb I had to change:

    def replace_nbsp(str)
      str.gsub([0xA0].pack('U'), ' ')
    end
    

    to

    def replace_nbsp(str)
      if str.respond_to?(:valid_encoding?)
        str.force_encoding('UTF-8').gsub(/\xc2\xa0/u, ' ')
      else
        str.gsub(/\xc2\xa0/u, ' ')
      end
    end
    

    There was also a patch submited to webrat Ticket 260, but it did not work for me so I had to do the above. Hope this helps.