views:

329

answers:

1

I am learning Cucumber and Webrat with Rails and would like some advice on the best way to test an "edit" form. When I browse to a user's profile I am presented with an edit form with the user's information pre-populated in the form fields. I would like to be able to test that the fields do in fact contain the information I expect. Here's my scenario:

  Scenario: View My Profile
    Given  I am logged in as "Mike" with password "secret"
    When I go to my profile page
    Then I should see "Mike" in the "Login" field
    And I should see "[email protected]" in the "Email" field
    And I should see a blank "Password" field
    And I should see a blank "Password confirmation" field

Cucumber tells me, correctly, that I need to define the following custom steps:

Then /^I should see "([^\"]*)" in the "([^\"]*)" field$/ do |arg1, arg2|
  pending
end

Then /^I should see a blank "([^\"]*)" field$/ do |arg1|
  pending
end

I'm sure I can figure out some nasty regex to implement evaluating these steps, but I feel there must be something already existing or more elegant that I can do. How do you evaluate forms with data pre-populated in the form fields?

+4  A: 

Have a look at features/step_definitions/webrat_steps.rb, the following step definition looks like what you are looking for:

Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
  field_labeled(field).value.should =~ /#{value}/
end
dhofstet
Dang. I'm not sure how I missed that! Based on this I created a version that works for empty fields. As in /^the "([^\"]*)" field should be blank$/" being implemented as "field_labeled(field).value.should == nil". Thanks!end`
SingleShot