There are different type of testing like unit testing, integration testing, functional testing etc.. Lets we talk about unit testing by using different testing tools and framework.
In ruby on rails there are two different way for testing.
1. Behaviour Driven Development (BDD)
2. Test Driven Development (TDD)
Behavior Driven Development (BDD):
BDD is about specifying the system behaviour by defining exectable specifications in the form of tests. It forces the developer to understand the behaviour of the method he is about to write. Using different tools, the specs written to test the application can be used as specifications.BDD makes it easier for developers to read and understand the code, becoming an additional form of documentation.
Test Driven Development (TDD):
TDD is about defining test cases before writing any other code. By creating unit tests you specify the minimum acceptable behaviour for the code. In TDD, we are usually writing one test per function, leading to many small tests. TDD written to use a more English like vocabulary for expressing functionality.
Now, We move forward to Behaviour Driven Development. There are many tools avaliable to implement BDD by using RSpec,Cucumber,Shoulda, etc..
How to install RSpec and steps to implement RSpec.
1. using gem
sudo gem install rspec
or using plugin
script/plugin install git://github.com/dchelimsky/rspec.git
script/plugin install git://github.com/dchelimsky/rspec-rails.git
2. When you are doing testing use test database.
3. Generate spec directory in root of application.
script/generate rspec
4. After installing gem it’s provides facility to generate models and test them.
script/generate rspec_scaffold post title:string body:text
5. Run the migration file.
rake db:migrate RAILS_ENV=test
6. Now, write the test case in spec/models/post_spec.rb
#include the spec_helper available in spec folder of your rails application.
require File.expand_path(File.dirname(FILE) + ‘/../spec_helper’)
describe Post do
before(:each) do
@post = Post.new(valid_post_hash)
end
it "should be valid" do
@post.should be_valid
end
it "should not be valid without a title" do
@post.title = ‘’
@post.should_not be_valid
end
it "should not be valid without a body" do
@post.body = ’’
@post.should_not be_valid
end
def valid_post_hash
{:title => ‘test post’, :body => ‘description for posted test’} #test data which you want to provide
end
end
7. All specs have a behaviour_type which determines what helper methods are available to you as you build your specs. You will change the explicitly one line in post_spec.rb is:
describe Post, "a important information", :behaviour_type => :model do
8. In post model, we have to define the validation for not null and unique title for post.
class Post < ActiveRecord::Base
validates_presence_of :title, :body
validates_uniqueness_of :title
end
9. You want to run the test case by giving command
rake spec
it run the test case and give the output either fail or pass.
=> Spork is a tool used to preload your environment so you don’t have to load it each time you run your features or specs.
- source: http://fr.ivolo.us/posts/rspec-tutorial-part-2-a-simple-test
Sorry, comments are closed for this article.