[rspec-users] Can't test for page content after a redirect

Zach Dennis zach.dennis at gmail.com
Thu Aug 7 14:54:48 EDT 2008


On Thu, Aug 7, 2008 at 2:44 PM, Christian Lescuyer <lists at ruby-forum.com> wrote:
> I'm trying to verify that data entered in a form has been saved in the
> database.
>
>  Scenario: User enters a bookmark URL
>    Given a bookmark 'http://www.gotapi.com/rubyrails'
>    When the user adds the bookmark
>    Then should redirect to '/'
>    And the page should contain 'http://www.gotapi.com/rubyrails'
>
> The first test (should redirect) passes. The second fails. I understand
> why: the page that is analysed is a dummy page "You are being
> redirected."
>
> Is it possible to check data in the second page?

What about something like:

  Scenario: User enters a bookmark URL
    Given I log in as a user
    When I add 'http://www.gotapi.com/rubyrails' as a bookmark
    Then I should see that has the 'http://www.gotapi.com/rubyrails'
has been added to my bookmarks

I'd imagine the steps to look something like:

    Given "I log in as a user" do
       # do whatever to login
    end

    When I add  $url as a bookmark do |url|
        # fill out the new bookmmark form with the given url
        # after submitting form handle redirects
        follow_all_redirects
    end

    Then "I should see that has the $url has been added to my
bookmarks" do |url|
       # response should have url somewhere on page
    end

follow_all_redirects looks like:

    def follow_all_redirects
      if response.content_type == "text/html"
        follow_redirect! while response.redirect?
      elsif response.content_type == "text/javascript"
        if md=response.body.match(/window.location.href = "([^"]+)"/)
          get md.captures.first
        end
      end
      true
    end



-- 
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com


More information about the rspec-users mailing list