DevOps | Software Automation | Continuous Integration

Category: Unit Tests

RSpec 2 – Stubs

Below is an example on how to use Stubs in RSpec 2:

context “percentage of review” do
     it “should be able to return the percentage of review” do
       section=Section.create(:section_config=>@section_config, :review => @review)
       item=Item.create(:section=>section)
       item.stub(:component_of_score_met).and_return(3.5)
       item.stub(:weighting_of_review).and_return(5.5)
       item.percentage_of_review.should==19.25
     end


  end

RSpec 2 – Mailer

Below is an example of a review_mailer_spec.rb:

describe ReviewMailer do
  it ‘creates and sends an email’ do
     @email=ReviewMailer.create_email(“test@com.au”,”Hello”,”Testing 123″)
     @email.to.should==[“test@com.au”]
     @email.subject.should==”Hello”
     @email.body.encoded.should match “Testing 123”
  end
end

RSpec 2 – Controllers

Below is an example of a behaviours_controller_spec.rb file:

describe ‘#update’ do
  
    it “should allow the rating of an item by the review person” do
      item=mock_model(Item)
      review=mock_model(Review)
      get :update, rating_id: review.id, id: item.id, rating: 4
      response.status.should==302


    end
  
    it “should allow the rating of an item by the review manager” do
       item=mock_model(Item)
       review=mock_model(Review)
       put :update, rating_id: review.id, id: item.id, rating: 4
       response.status.should==302
    end
  
  end

RSpec 2 – Model

Below is an example of a RSpec 2 – Model test in a item_spec.rb file:

context “maximum score” do
     it “should return maximum score of 0 when there is no item ratings” do
       section=Section.create(:section_config=>@section_config, :review => @review)
       item=Item.create(:section=>section)
       item.maximum_score.should==0
     end


     it “should return the maximum score when there is an item rating” do
       ItemRating.create(title: ‘High’, value: 4, :section_config => @section_config)
       ItemRating.create(title: ‘High’, value: 8, :section_config => @section_config)
       section=Section.create(:section_config=>@section_config, :review => @review)
       item=Item.create(:section=>section)
       item.maximum_score.should==8
     end
  end

RSpec 2 – Requests

Below is an example of a RSpec 2 – Request test looks like in a  behaviours_spec.rb file:

describe “Behaviours” do
  describe “GET /behaviours” do
    it “works! (now write some real specs)” do
      get behaviours_path
      response.status.should be(302)
    end
  end
end

RSpec 2 – Views

Below is an example of a RSpec 2 View test looks like in a html.erb_spec.rb file:

it “renders the edit framework form” do
    render


    assert_select “form”, :action => frameworks_path(@framework), :method => “post” do
        assert_select “input#framework_title”, :name => “framework[title]”
    end
  end
end

RSpec 2 – Routing

Below is an example of how a routing RSpec test is written for a Rails 3 application

In spec/routing/behaviours_routing_spec.rb

describe Admin::BehavioursController do
  describe “routing” do


    it “routes to #index” do
      get(“/behaviours”).should route_to(:controller => “admin/behaviours”,:action => “index”)
     end
end





Hint: “rake routes” shows all the routes in the application

© 2023 Chuan Chuan Law

Theme by Anders NorenUp ↑