tdd with capybara

 

1. add capybara to the Gemfile

 

 

group :test do 
  gem 'capybara','1.1.2'
end

 

 

2. execue the command below, it will replace Test::Unit with Rspec, when we new an application ,rails will

    generate the test::unit , so we should skip it first

$ rails generate integration_test static_pages

 

$ rails new sample_app --skip-test-unit

 

$ rails generate rspec:install

 

3. when we use rails g controller ... , rails will also generate Rspec testcase for us, we can skip it too.

 

$ rails generate controller Home index --no-test-framework

 

4. generate integration test(集成测试) by ourselves

 

$ rails generate integration_test static_pages

 

5. write the testcase

 

require 'spec_helper'

describe "StaticPages" do

let(:base_title) {"Ruby on Rails Tutorial Sample App"}

  describe "Home page" do
    it "should have the content 'Sample App'" do
       visit "/static_pages/home"
       page.should have_content('Sample App')
    end       
    
    it "should have the h1 'Sample App'" do 
      visit "/static_pages/home"
      page.should have_selector('h1',:text => "Sample App")
    end

    it "should have the title 'Home'" do
      visit "/static_pages/home"
      page.should have_selector('title',:text => "#{base_title} | Home") end
  end
  
  describe "Help page" do 
    it "should have the content 'Help'" do 
      visit "/static_pages/help"
      page.should have_content('help')
    end
  
    it "should have the h1 'Help'" do
      visit "/static_pages/help"
      page.should have_selector('h1', :text => "Help")
    end

    it "should have the title 'Help'" do 
      visit "/static_pages/help"
      page.should have_selector('title', :text => "#{base_title} | Help")
    end
  end

  describe "About page" do 
    it "should have the content 'About us'" do 
      visit "/static_pages/about"
      page.should have_content('About Us')
    end

    it "should have h1 title 'About'" do
      visit "/static_pages/about"
      page.should have_selector("h1", :text => "About")
    end

    it "should have the title 'About'" do
      visit "/static_pages/about"
      page.should have_selector("title", :text => "#{base_title} | About")
    end
   end 
  end

 

6. execue the test, then modify our code to pass the it.

 

$ bundle exec rspec spec/requests/static_pages_spec.rb

 

你可能感兴趣的:(test,captbara)