capybara & Rspec-1

 

Rspec基本操作

1. let 设置变量

2. subject 定义测试对象

3. before 在执行测试用例it之前的操作

 

require 'spec_helper'

describe "StaticPages" do

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

  subject { page }

  describe "Home page" do
    before { visit root_path }
    it { should have_content('Sample App') }
    it { should have_selector('h1',:text => "Sample App") }
    it { should have_selector('title',:text => full_title(""))}
  end

  describe "Help page" do
    before { visit help_path }
    it { should have_content('help') }
    it { should have_selector('h1', :text => "Help") }
    it { should have_selector('title', :text => full_title("Help")) }
  end

  describe "About page" do
    before { visit about_path }
    it { should have_content('About Us')}
    it { should have_selector("h1", :text => "About")}
    it { should have_selector("title", :text => full_title("About"))}
   end
  end

 

 5. 注意full_title方法,定义在spec/support/utilities.rb文件中,Rspec会自动加载spec/support下得文件

 

# spec/support/utilities.rb

def full_title(page_title)
  base_title = "Ruby on Rails Tutorial Sample App"
  if page_title.empty?
    base_title
  else
    "#{base_title} | #{page_title}"
  end
end

 

你可能感兴趣的:(rspec,capybara)