1
votes

I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. By using Capybara I would like to check if a HTML form is submitting to the proper URL; that is, to check if the related action="<PATH>" HTML "tag"/"property" is what I expect it to be.

For example, in the following code I would like to check if the <PATH> is /users (the full HTML is action="/users") where the Ruby on Rails route is new_user_path:

<form method="post" id="css_form_id" action="/users">
  ...
</form>

At this time, in order to check if the form is present on the page, I am using the following code:

Then /^I should see the form$/ do
  page.should have_selector('form#css_form_id', :visible => true)
end

Is it possible to check if the form is submitting to the proper URL? If so, how can I make that? What do you advice about?

1

1 Answers

2
votes

I think you are missing a fundamental concept of behavior driven development. Do you really care what the form is called or where it is sending data? Not when you're talking about the high level behavior of your application. You should be testing that the application is doing what it is supposed to do when the form is submitted. Rather than checking which path it is posting to, you should be testing that when the form is submitted, the outcome is the expected one. It looks like you are creating a new user with this form, so I would check that a new user has been created with the parameters you submitted:

When I create the following user:
  | Name  | Some Person      |
  | Email | [email protected] |
Then I should have the following user:
  | Name  | Some Person      |
  | Email | [email protected] |

Your assertion step would then just check that the user has been persisted between steps

Then /^I should have the following user:$/ do |user_table|
  expected_user = user_table.rows_hash
  user = User.find_by_email(expected_user['Email'])
  user.name.should == expected_user['Name']
end

Another reason not to test your views like that is because it makes your tests too brittle. If you change a class or id for the form, your tests will break, even though the behavior hasn't changed at all.