OK now I think you all have good knowledge about my pasts posts. I f you have any question you can ask me any time by mailing me. Now lets move to our todays lesson.
What can we do further with our project. Hmmmmm... We can validate user inputs . Lets see how can the validations can be done using cucumber.
What can we validate. We can validate “user inputs are OK such as is price is a numerical value or not and all the fields are not empty etc..”
Before doing the cucumber part lets see the what is happening in GUI part.
Go to the product/app/models/ folder and open product.rb file. Afetr that copy and paste following codes to it as follows.
class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
end
Now lets see the GUI.
1)First go to the product folder in the terminal and then run the server “ruby script/server”
2) Then gotothe browser and type “http://localhost:3000/products” . Then you can see following GUI
3)click on New product link and you can see this page.
4)Now check what is happening when click on create button without filling any field. You can see following error massages.
5)Now you can see what is happening when there is one field is filling and click create button. Check them with your self.
6)Now lets see how the cucumber can use for this.
7)Go to the feature folder and create a new file called validate_product_details.feature and copy and paste following scenario.
8)
Scenario: Validate Product Details
Given I have no products
And I am on the list of Products
When I follow "New Product"
And I fill in "Title" with ""
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with ""
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"
9)Now run the cucumber features/validate_product_details.feature then you can see following.
10)It says all steps are passed. In this scenario we can check alll the validation by writing steps like follows
Scenario: Validate Product Details
Given I have no products
And I am on the list of Products
When I follow "New Product"
And I fill in "Title" with ""
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with ""
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"
When I fill in "Title" with "C#"
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with ""
And I press "Create"
Then I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"
When I fill in "Title" with "C#"
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with "23"
And I press "Create"
Then I should see "Description can't be blank"
And I should see "Image url can't be blank"
When I fill in "Title" with "C#" .......
like wise we can write so many steps.
11)But you can see that is not much effective because write all the validation like this is not good. What can we do for it. Think a little bit....
12)If you can remember the last post why we can't use that. Then the above scenario can be written as follows.
Scenario: Validate Product Details
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 | |
| Description | |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"
13)Now run the cucumber features/validate_product_details.feature
href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaQD7rNpHoYBhgzg21XQiHOEH_8Jh02dNuAdP0oKgQBuNYlolS3o01DToXvYZ3QdZIj_HiUdi5RtfodqhQLnUXivHK4TiiWY3fuRYCIqvfi0SIGn6X5UacXBI-qvxxAgK5XghdRY7HIXQ/s1600-h/5.png">
14)What a simple way to write validation file. Now lets expand this as follows and try to understand to what is happening there.
Scenario: Validate Product Details
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 | |
| Description | |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"
When I fill in the following:
| Title | |
| Description | |
| Image Url | |
| Price | 20 |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
When I fill in the following:
| Title | |
| Description | |
| Image Url | a.jpg |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Price is not a number"
When I fill in the following:
| Title | |
| Description | My name is JTK |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"
When I fill in the following:
| Title | JTK |
| Description | |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"
15)Now run the cucumber features/validate_product_details.feature now try to write your own validation files for your projects.
Cucumber and Ruby on Rails (Part 6) My learning materials
Posted by ජීවන්ත තාරක (Jeewantha Tharaka Kodikara) at 12:54 PM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment