Links to My Posts

Labels

Blogumulus by Roy Tanck and Amanda Fazani
free counters

How many in online

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

Now lets start to create scenarios and do testing using cucumber

1)Lets create our first scenario as follows and save it in the product/features folder as named
list_of_product.feature

Scenario: Products List
Given I have products titled Java, description Platform Independent, Image url java.jpg, Price 20
When I go to the list of Products
Then I should see "Java"
And I should see "Platform Independent"
And I should see "java.jpg"
And I should see "20"

2)Now run the cucumber features and see the out put as




3)You get 1 scenario (1 undefined)
6 steps (5 skipped, 1 undefined)
that means your 1st step is not defined. That means the cucumber do not know how to deal with the step

GivenI have products titled Java, description Platform Independent, Image url java.jpg, Price 20

To deal with this that should be in the webrat_step.rb file in the features/step_definitions folder. If not we have to create them for the use of cucumber and you can see the cucumber suggest you some definitions as

Given /^I have products titled Java, description Platform Independent, Image url java\.jpg, Price 20$/ do
pending
end



4)Now lets create the definition file. Create a file named product_step.rb file in the features/step_definitions folder and paste the following code there and save it.
Given /^I have products titled (.+), description (.+), Image url (.+), Price (.+)$/ do |title,description,image_url,price|

Product.create!(:title => title, :description => description, :image_url => image_url, :price => price)

end


In this step it says to store the data in the table called Product.

5)Now lets run again cucumber features and see the result. You can see following error.



This says there is no such a constant like Product. This means still we do not create our Product Table lets create our product table. Type on the terminal as follows.

ruby script/generate scaffold product title:string description:text image_url:string price:decimal

rake db:migrate


6)Now lets run again cucumber features



That says that product_test.products table. That because these scenarios working with the tables in the test database. So lets fix it. Run following code.

rake db:test:clone

Now lets run again cucumber features.

7)Its hows the following error and add it to the file path.rb in features/support

this should come after case statement

when /the list of Products/

products_path



the reason for this is (

We have our first passing step! The second step is now failing, though. This is because Cucumber doesn’t know how to translate “the list of articles” into a path in our application.
One of the files installed by Cucumber is called paths.rb, which lives in the /features/support directory. In this file we can define custom paths that map the English definitions in Cucumber files to paths in our Rails application. To add a mapping we just add a new when condition to the case statement in the path_to method. (There’s a comment in the file to show us where to add it.)
)
8)Now run again cucumber features



9)You get all the scenarios are passed.
10)Now our first scenario is passed lets try another scenario in the next post. Try this scenario and check you can understand it. Copy and paste to the same feature file

Scenario: Products List
Given I have products titled Java, description Platform Independent, Image url java.jpg, Price 20
And I have products titled Ruby on Rails, description Rich internet Application, Image url ror.gif, Price 25
When I go to the list of Products
Then I should see "Java"
And I should see "Platform Independent"
And I should see "java.jpg"
And I should see "20"
Then I should see "Ruby on Rails"
And I should see "Rich internet Application"
And I should see "ror.gif"
And I should see "25"

2 comments:

Anonymous said...

Jeewantha,

Great Intro post :)

Rather than defining Given step data in each step consider using multiline step arguments using tables.

Also, using a data factory such as machinist or FactoryGirl (instead of ActiveRecord::create) would ease the process of preparing test data.

Anyway, keep the good stuff coming :)

ජීවන්ත තාරක (Jeewantha Tharaka Kodikara) said...

Hi,
Thanks for the coment. Actuallly i havent any knowledge about FactoryGirl and these days iam sudding on that.
lets see what can do for next post.

cheers,
jeewantha.