rspec 2.5 with jruby 1.5.6 + rails 3

Posted: May 30, 2011 in Jruby, Ruby On Rails
Tags: , , ,

 

Here is one another post on rspec 2.5 with jruby 1.5.6 + rails 3

Installation steps :

First of all I consider you have setup environment with jruby 1.5.6 + rails 3.0.6.

You can download jruby 1.5.6 zip file from following link

http://jruby.org.s3.amazonaws.com/downloads/1.5.6/jruby-bin-1.5.6.zip

After environment  setup and configuration.

Step 1 -  To install rspec gem just add following lines in your Gemfile of your project.\

group :development, :test do
  gem "rspec-rails", ">= 2.0.1"
end

Step 2 - Try bundle install at project root

> bundle install

Step 3 -  Once you installed, then you need to install RSpec

> jruby -S  rails  g  rspec:install

Output :

exist  lib
create  lib/tasks/rspec.rake
exist  config/initializers
create  config/initializers/rspec_generator.rb
exist  spec
create  spec/spec_helper.rb

Step 4 – install binstubs in your project.

> bundle install --binstubs

This will create bin folder in your project directory ( like  urproject/bin/ )

and will auto generate all rspec bin stubs into that directory.

Like..

autospec
cdiff
cucumber
decolor
edit_json.rb
erubis
htmldiff
image_voodoo
ldiff
memcached_top
mongrel_rpm
newrelic
newrelic_cmd
nokogiri
prettify_json.rb
rackup
rails
rake
rake2thor
rprotoc
rspec
ruby_parse
thor
tilt
tt

Step – 5  - Create test database

> rake db:test:clone_structure

So this is all about rspec installation and configuration for your project.

 

Step – 6Unit testing ( Model testing)

# spec/models/user_spec.rb

require File.dirname(__FILE__) + '/../spec_helper'

context "User" do
  before(:each) do
   @user1 = User.new(:name => "Ganesh")
   @user2 = User.create(:name => "Ganesh K", :city => "Mumbai", :country => "India")
  end

  specify "should save" do
    lambda {
      @user1.city = "Navi Mumbai"
      @user1.country = "India"
      @user1.save.should be_true
    }.should change(User, :count).by(1)
  end
  specify "should not save" do
    lambda {
      @user1.save.should be_false
      @user1.errors.should_not be_empty
    }.should change(User, :count).by(0)
  end

  specify "should update" do
   lambda {
      @user2.city = "Pune"
      @user2.save.should be_true
      @user2.city.should == "Pune"
    }.should change(User, :count).by(0)
  end  

  specify "should destroy" do
    lambda {
      @user2.destroy.should be_true
    }.should change(User, :count).by(-1)
  end
end

 

Step – 7  – Functional Testing ( Controller Testing)

# spec/controllers/users_controller_spec.rb

require File.dirname(__FILE__) + '/../spec_helper'

describe UsersController do
  render_views

  before(:each) do
   @user1 = User.new(:name => "Ganesh")
   @user2 = User.create(:name => "Ganesh K", :city => "Mumbai", :country => "India")
  end

  specify "should be show index" do
    get :index
     response.should be_success
     assigns[:users].should_not be_blank
    end
  end

  specify "should create user." do
   lambda {
     post :create, {:user => {:name => "Ganesh", :city => "Latur", :country => "India"} }
     assigns[:user].should_not be_blank
     response.should redirect_to(users_path)
   }.should change(User, :count).by(1)
  end

  specify "should update user." do
   lambda {
     put :update, {:id => @user2.id, :user => {:city => "Pune"}}
     assigns[:user].should_not be_blank
     assigns[:user].city.should == "Pune"
     response.should redirect_to(users_path)
   }.should change(User, :count).by(0)
  end
  specify "should destroy user." do
    lambda {
     post :destroy, {:id => @user2.id}
     response.should redirect_to(users_path)
    }.should change(User, :count).by(-1)
  end
end

======================================

You can refer following few rspec command to test your model / controller files are like.

To test model

> jruby -S bin/rspec spec/models/user_spec.rb

To  test controller

> jruby -S bin/rspec spec/controllers/users_controller_spec.rb

To test all model spec files with single command.

> jruby -S bin/rspec spec/models

To test all controller spec files with single command.

> jruby -S bin/rspec spec/controllers

Cheersss!!!!

-Ganesh K

Advertisement
Comments
  1. sumit says:

    Thanks for the post!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s