Links to My Posts

Labels

Blogumulus by Roy Tanck and Amanda Fazani
free counters

How many in online

Cucumber and Ruby on Rails (Part 5) My learning materials

OK now I think you have a good idea of my past posts. Pardon me because my new post is late. As I said earlier there is a simple way of the last scenario. First we look at that scenario.

Scenario: Create Valid Product
Given I have no products
And I am on the list of Products
When I follow "New Product"
And I fill in "Title" with "Ruby On Rails"
And I fill in "Description" with "Model View Control Architecture"
And I fill in "Image Url" with "ror.jpg"
And I fill in "Price" with "22"
And I press "Create"
Then I should see "Product was successfully created."
And I should see "Ruby On Rails"
And I should see "Model View Control Architecture"
And I should see "ror.jpg"
And I should see "22"
And I should have 1 product

In this scenario we used “I fill in “(field name)” with “(value)” ” lot of time. But we can give this in a simple way. Lets look at that scenario.

Scenario: Create Valid Product
Given I have no products
And I am on the list of Products
When I follow "New Product"
When I fill in the following:
| Title | C# |
| Description | ABC |
| Image Url | c.jpg |
| Price | 5 |
And I press "Create"
Then I should see "Product was successfully created."
And I should see "C#"
And I should see "ABC"
And I should see "C#"
And I should see "5"
And I should have 1 product

When we write this what is happening. Go to your webrat_step.rb file and you can see the following step definition.

# Use this to fill in an entire form with data from a table. Example:
#
# When I fill in the following:
# | Account Number | 5002 |
# | Expiry date | 2009-11-01|
# | Note | Nice guy |
# | Wants Email? | |
#
# TODO: Add support for checkbox, select og option
# based on naming conventions.
#

When /^I fill in the following:$/ do |fields|
fields.rows_hash.each do |name, value|
When %{I fill in "#{name}" with "#{value}"}
end
end

There you can see the the cucumber give a easy way to do our job without typing same thing several times like in the 1st scenario.

What is happening from this step definition is it created a step for us by getting the values in table. How it does lets look at step by step.

When /^I fill in the following:$/ do |fields|
From this it takes the one field of the raw for a example | Title | C# |

fields.rows_hash.each do |name, value|
From this it takes the values of the field as name and value then
name= Title
value=c#

When %{I fill in "#{name}" with "#{value}"}
From this step its create a step using that value then the step comes as
When I fill in Title with C#
and end like wise all the fields are translated by the cucumber.

Then what happen. Now it check this new step with the webrat file or with our step definition file and go through it.

The correct step definition is

When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
fill_in(field, :with => value)
end

When we run this scenario using cucumber features/create_product.feature we got as follows.



it says all the steps are pass. that means code is OK. see how the cucumber is easy to work. according to that you can type these steps as table in that scenario

Then I should see "Product was successfully created."
And I should see "C#"
And I should see "ABC"
And I should see "C#"
And I should see "5"

try it your self.

Best Regards,
Jeewantha.