Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Need to understand the Rspec syntax with Rails

rspec rails

  • Please log in to reply
1 reply to this topic

#1 arup

arup

    Passenger

  • Members
  • 1 posts

Posted 11 January 2014 - 08:09 AM

Hi,

 

I am very new to rails and Rspec. I was following the tutorial - http://ruby.railstut...c-pages#sec-TDD. The author wrote the test code as below :

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end

  describe "Help page" do

    it "should have the content 'Help'" do
      visit '/static_pages/help'
      expect(page).to have_content('Help')
    end
  end
end

Now my question is why is it needed to put `describe "Home page"` and `describe "Help page"` block inside the `describe "Static pages"` block. Why are not we put the inside block on the top level ieself ?



#2 RoyTheB0y

RoyTheB0y

    Signalman

  • Members
  • 10 posts

Posted 11 January 2014 - 08:53 AM

I think its just a matter of organisation and making the output readable

 

Test Results

   Static pages

      Home page

           it should have content Sample App

      Help page

           it should have content Help

  Buy Products

       Shopping cart

           it should sum purchased product costs

 

is more useful than

 

Test Results

it should have content Sample App

it should have content Help

it should sum purchased product costs

 

 

Also Buy Products tests would need a different set of conditions to be set up than Static pages, these can be set up within the describe block and then applied to all tests within this block, you wouldn't want these conditions to be set up to test the Static Pages. (eg  Buy Products needs products to be set up and the shopping cart to be populated which you don't need to do to test the Static ages)

 

describe 'Buy Products'

  before(:each) { .. set up a load of conditions relevant to buying products ... }

  it ("should sum purchased product costs") {







Also tagged with one or more of these keywords: rspec, rails

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users