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.