TDD (Rspec) Setup in Rails Application
How to set-up TDD environment :
Test Driven Development :
Test Driven Development is a programming practice that instructs “Developers to write new code only If the automated test cases failed and to eliminate duplication?
For every bit of functionality, you first develop a test that specifies and validates what the code will do”
Goal :
Clean code that works
How to do Rspec in rails application:
1) Create a Rails Project :
Create a rails project and go inside the project directory
a) rails new test_app -d mysql
b) cd test_app
2) Configure Your Test Database for Test Driven Development
In config/database.yml :
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: msp_test_app
pool: 5
username: cyncable
password: cyncable
host: localhost
3) In Gem file :
- gem “rspec”
- gem “rspec-rails”
Add the following gems for Rspec environment
4) Install gems :
Run the following command to install gems in your application directory
- bundle install
5) Bootstrap Rspec :
Run the following command to Initialize the?spec/?directory (where specs will reside) with:
- rails generate rsepc:install
Example :
kpradeep@pradeepk:~/projects//test_app$ rails g rsepc:install
exists lib/tasks
create lib/tasks/rspec.rake
create script/autospec
create script/spec
create script/spec_server
create spec
create spec/rcov.opts
create spec/spec.opts
6) Generate Model, Test, and Migration with rspec_model
6.1) create new model
kpradeep@pradeepk:~/projects/test_app$ rails g model ProfileInformation
invoke active_record
create db/migrate/20130508124121_create_profile_informations.rb
create app/models/profile_information.rb
invoke test_unit
create
test/unit/profile_information_test.rb
create
test/fixtures/profile_informations.yml
6.2)* create new Rspec model*
To generate the Rspec model for newly created model.Run the following command, it will create new file under spec/models
- rails g rspec:model model_name
Example :
kpradeep@pradeepk:~/projects/test_app$ rails g rspec:model ProfileInformation
create spec/models/profile_information_spec.rb
7) create new Controller :
kpradeep@pradeepk:~/projects/test_app$ rails g controller ProfileInformations
create app/controllers/profile_informations_controller.rb
invoke erb
create app/views/profile_informations
invoke test_unit
create test/functional/profile_informations_controller_test.rb
invoke helper
create app/helpers/profile_informations_helper.rb
invoke test_unit
create
test/unit/helpers/profile_informations_helper_test.rb
invoke assets
invoke coffee
create
app/assets/javascripts/profile_informations.js.coffee
invoke scss
create
app/assets/stylesheets/profile_informations.css.scss
7.1) Generate Rspec controller
Run the following command, it will create new file under spec/controller
- rails g rspec:controller controller_name
Example :
kpradeep@pradeepk:~/projects/test_app$ rails g rspec:model ProfileInformation
create spec/controllers/profile_informations_controller_spec.rb
8) Migrate the DB
kpradeep@pradeepk:~/projects/test_app$ rake db:migrate
9) Prepare the Test database
kpradeep@pradeepk:~/projects/test_app$rake db:test:prepare
10) Run Rspec
kpradeep@pradeepk:~/projects/test_app$ rspec spec
it runs the all specs available under specs folder.If u want to run rspec for specific areas like controllers or models can use the
following commands
Example :
Run the rspec test cases under Controllers folders
- rspec spec/controllers
Run the specific controller test cases
- rsepc spec/controllers/profile_informations_controller_spec.rb
Run the rspec test cases under Model folders
- rspec spec/models
Run the specific model test cases
- rsepc spec/models/profile_information_spec.rb
Comments
Thanks dude….Hereafter i will be keep on posting…………..
Great buddy. Good to see your blogs. Keep posting. :)