Skip to content


Cucumber

Cucumber is a fairly easy BDD tool that I’ve been using for the last month or so, but there have been a couple things I’ve had to learn on my own to make some of the webrat stuff work, so I thought I’d share one instance of what I’ve been doing.

To start, lets show the feature. I’ll go through each line one at a time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    Scenario: Create a new style from scratch
        Given I have logged in as "testuser"
        And I am on the style creation page
        And I fill in the fields
            | unit_style_code        | S100  |
            | unit_style_description | A simple floorplan |
            | unit_style_square_feet | 100.0 |
            | unit_style_price       | 1000.0 |
        When I press "Save"
        Then with the latest unit_style
        Then I should be on the edit style page for that unit_style
        Then the "unit_style[code]" field should contain "S100"
        Then the "unit_style[description]" field should contain "A simple floorplan"
        Then the "unit_style[square_feet]" field should contain "100.00"
        Then the "unit_style[price]" field should contain "1000.00"

Line 2: This one was tricky. I’m using restful authentication for rails and I’m using users already created with dummy/test account information for testing purposes. The “I have logged in as” step does all the logging in steps. Getting through all that is another posts (one shorter than this one tho).

Line 3: Just create the mapping for “the style creation page” in /features/support/path.rb

Line 4-8: In /features/step_definitions/webrat_steps.rb, I put in the following code:

1
2
3
4
5
When /^I fill in the fields$/ do |table|
  table.raw.each do |row|
    fill_in(row[0], :with => row[1])
  end
end

Line 9: “Save” is the name of the submit button. The “I press” step is already in the generated webrat_steps.rb

Line 10: I created a unit_style_steps.rb, which contains:

1
2
3
Then /^with the latest unit_style$/ do
  @unit_style = UnitStyle.find(:last)
end

Line 11: Again, back to path.rb, this time I added the following:

1
2
    when /the edit style page for that unit_style/
      "/housing/engineering/edit_style/" + @unit_style.id.to_s

Line 12-15: This is where I was having problems. The generated webrat_steps had the following for it:

1
2
3
Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
  field_labeled(field).value.should =~ /#{value}/
end

But it wasn’t finding the fields correctly. So after using the save_and_open webrat function (now another method in my webrat_steps) I was able to determine that the fields were there and that they had the correct information in them. Webrat just was not finding them. After a bit more digging I found the file_named function, and now, the step looks like this:

1
2
3
Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
  field_named(field).value.should =~ /#{value}/
end

So, with only a few lines of code, I have a simple, easy to read scenario that just about any user would be able to read and understand, but also something I can take and run on a regular basis to make sure the application is also running the way it should.

Posted in programming.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.