Rails 3 compatibility solutions

144 views
Skip to first unread message

yannimac

unread,
Jul 29, 2010, 1:56:28 PM7/29/10
to webrat
I have been working with Cucumber and webrat all morning to try to get
things to run correctly under Rails 3. I am not sure if this has been
addressed on this board, but I just wanted to point out some fixes for
those who are struggling with this.

I first started with a solution that someone posted about using the
rack config instead of the rails config and adding the webrat methods
and matchers in env.rb:
------------------------
require 'webrat'
require 'webrat/core/matchers'

Webrat.configure do |config|
config.mode = :rack
config.open_error_files = false # Set to true if you want error pages
to pop up in the browser
end

World(Webrat::Methods)
World(Webrat::Matchers)
-----------------------------

This works for most stuff except the "redirect_to" method in rails.
It does not follow the redirect. The problem is that rack sets the
internal URL to "example.org" and rails sets it to "www.example.com".
Webrat is looking for example.com, so the redirects are not followed.
Here is a patch:

#/lib/webrat/core/session.rb
#starting at line 288
def current_host
- URI.parse(current_url).host || @custom_headers["Host"] ||
"www.example.com"
+ URI.parse(current_url).host || @custom_headers["Host"] ||
default_current_host
end

+ def default_current_host
+ adapter.class==Webrat::RackAdapter ? "example.org" :
"www.example.com"
+ end

The redirects work after that, but I wanted to get the rails adapter
working with the default env.rb file. So I removed the stuff I added
with rack, etc.. and I used this:

#env.rb
require 'webrat'
require 'webrat/core/matchers'

Webrat.configure do |config|
config.mode = :rails
config.open_error_files = false # Set to true if you want error pages
to pop up in the browser
end

Then I had to make a few additional changes b/c Rails moved some code
out of action_controller into action_dispatch and deprecated some of
the classes. The files that I changed were:

/lib/webrat/integrations/rails.rb
/lib/webrat/core/elements/form.rb

I looked around on github afterwards to see if there were any good
forks for rails 3 and I found one that had more elegant patches than I
coded, so you should probably look at the commits made by kalv
http://github.com/kalv/webrat/commits/master

Hope this helps.

locksmithdon

unread,
Aug 19, 2010, 10:38:40 PM8/19/10
to webrat
Very helpful. Thanks!

Glenn

unread,
Sep 3, 2010, 8:28:54 PM9/3/10
to webrat
Also worked for us, thanks very much.

On Jul 29, 10:56 am, yannimac <john.mcali...@gmail.com> wrote:

robodo

unread,
Oct 18, 2010, 5:37:28 AM10/18/10
to webrat
Works for me as well. For those interested, here is my fork:

http://github.com/orangewise/webrat

#Gemfile
gem 'webrat', '>=0.7.2.beta.6', :git => 'git://github.com/orangewise/
webrat.git'


Thanks!

marco

unread,
Nov 22, 2010, 11:30:54 AM11/22/10
to webrat
Hello,

I am facing this exactly same problem... could some plelase help me:
where can I find this file?
/lib/webrat/core/session.rb

I coudn't find it, there's no webrat folder in my lib.

By the way I use RVM.

I would appreciate any help.

Regards.
Reply all
Reply to author
Forward
0 new messages