Testing In Rails: Introduction and Creating Test Data
Hi friends,
Even if you write code considering every case in mind there is always a chance that your code contains bugs. So the best way of writing a code that contains least number of bugs is writing a code along with its test cases. In this blog I am going to give you a brief idea about how we can write test cases in rails application without using any major third party software.
Rails has a built in framework of test cases that makes it really very easy to write cases. Once you create any component in rails including either application, controllers or models using its generators, it automatically creates supported test files, where you can write your test cases. For example if you run
> rails new MyTestingApp
# Along with the whole project directory it will also create a section for test like:
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
# Similarly during controllers:
> rails g controller welcome
# Along with controller and view folder it will create the following section too:
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
Thus we can see that as soon as we create an application, rails promotes us to write test cases. As we all know rails has different environment for running the application. i.e. Development, Production and Testing. So the testing enviroment is run while running the test cases. It also has separate database that can be defined inside the config/database.yml. A different database allows you to freely test the application without having any tension related to data loss or misuse. If you closely look at the test folder it will have different components that are present in your application. i.e. controllers, helpers, mailers, test_helper.rb, fixtures, integrations and models. In these files test_helper.rb is used for storing the default configurations for all the other files.
Creation of Test Data:
Creation of test data can be done using different techniques, one of them that are by default present with your application is using fixtures, which are used for storing data. They are actually a YAML files that can store some test data, they are associated with models and used to populate data from yaml files to tables before you run the test cases. As soon as you create a model using generator, an associated fixture file is generated with the model. i.e.
> rails g model MyProduct title:string description:string price:string
#=>
invoke active_record
create db/migrate/20160623174156_create_my_products.rb
create app/models/my_product.rb
invoke test_unit
create test/models/my_product_test.rb
create test/fixtures/my_products.yml
If we open my_products.yml, we can see it already created two test records for my_products table.
one:
title: MyString
description: MyString
price: MyString
two:
title: MyString
description: MyString
price: MyString
Now when you run a test cases it will actually perform these 3 tasks: a) Remove all the existing data from the table b) Import the data from the yaml file into the table c) Store the fixture data in a table to use it directly. They can also be accessed like an active record object:
my_products(:one)
#=> will return an object of MyProduct with name one
One more way of creating test data is using factory_girl_rails gem, that is actually a replacement for fixture data. It provides a much easy of creating data just like you do with models. For using it you need to add the gem file in the test group in your Gemfile like this:
group :test do
gem 'factory_girl_rails'
end
# after that run bundle
> bundle
A sample factory_girl file is like this:
FactoryGirl.define do
factory :my_product do
title "MotoG"
description "Mobile Phone"
price 100
end
end
Now to create a my_product you can simply write:
product = build(:my_product)
#This will initialize a MyProduct object
product = create(:my_product)
#This will create a MyProduct object
For more information regarding the use of factory_girl and its configuration you can go through the link Factory Girls Configurations.
Thus we are done with creating sample data in test cases, In the next chapters we will see how we can test our models using unit test cases and also will talk about testing the controller, links for them are given below:
Testing In Rails: Unit Testing In Models
Testing In Rails: Functional Testing For Controllers
0 Comment(s)