8

I'm writing an integration test for a rails application using webrat. After filling out a form, the user presses submit and an account is created.

click_button "Submit"
assert_contain "Your Account Has Been Created"

However, the test fails:

expected the following element's content to include "Your Account Has Been Created":
You are being redirected.
<false> is not true.

Normally to follow a redirect I would use post_via_redirect, but from just looking at Webrat's examples, click_button followed by assert_contain should work

I just started using Webrat, so am I missing something obvious here? Why am I stuck with the redirect response?

Thanks!

Deb

deb
  • 12,326
  • 21
  • 67
  • 86

4 Answers4

11

With a new Rails 3 app, I also had this problem testing a simple method which included a redirect_to call in the controller. The method itself worked fine, but Webrat would return the "You are being redirected." response.

Adding in a 'Then show me the page' step in cucumber (so the page that webrat sees opens in the browser) showed the 'You are being redirected." response with a link to an example.org link.

Based on this I discovered Yannimac's patch ( http://groups.google.com/group/webrat/browse_thread/thread/fb5ff3fccd97f3df ):

#/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 

Making these changes fixed the issue, so redirect_to calls with Webrat now work correctly.

justsee
  • 933
  • 7
  • 16
  • 2
    you can also use `follow_redirect!` – Rob Dec 03 '10 at 16:40
  • There is a problem with this... its not quite 100% it doesn't catch `redirect_to :back` – Rob Dec 03 '10 at 17:39
  • Thank you so much! A great solution to my problemo. – JZ. Mar 19 '11 at 19:32
  • visit choose_profile_path(:profile_name => @profile.name) follow_redirect! assert_contain @profile.name – Denis Apr 13 '11 at 13:04
  • [Here's a patch](https://github.com/brynary/webrat/pull/52/files) which is supposed to fix the problem. However, it introduces a new error for me (using rspec). – seb Mar 07 '13 at 21:19
3

There are some issues with rails 3 and webrat. Please see:

http://baldowl.github.com/2010/12/06/coercing-cucumber-and-webrat-to-cooperate.html

gdelfino
  • 11,053
  • 6
  • 44
  • 48
  • Works great for me in mode `:rack`. The essence is setting DEFAULT_HOST in Rack::Test which can be done easily in `env.rb`. It's trivial and maintainable, contrary to hacking webrat. – skalee Mar 03 '11 at 13:17
0

Do you have any authentication in your apps? I presume the redirection is because of you have not been authenticated. If my assumption is right, write a setup to login first with Webrat.

Joshua Partogi
  • 16,167
  • 14
  • 53
  • 75
0

Here is the gist with exactly what you need to do to solve this problem.

https://gist.github.com/752766

John Hinnegan
  • 5,864
  • 2
  • 48
  • 64