2013-10-11 85 views
1

我試圖創建一個簡單的rspec,檢查一個Bootstrap導航欄和它內部的相應鏈接。我在spec_helper.rb中包含了capybara/rspec,並且在spec/features目錄中有rspec,所以我認爲我的設置正確,但運行該規範會產生錯誤,指出內部方法未定義。使用水豚和rspec未定義的方法'內'

require 'spec_helper' 

feature "HomePage" do 
    before { visit root_path } 

    subject { page } 

    describe "the navigation bar" do 
    it { should have_selector('.navbar') } 

    within ".navbar" do 
     it { should have_link('Game Contest Server', href: root_path) } 
     it { should have_link('Users', href: users_path) } 
     it { should have_link('Sign Up', href: signup_path) } 
    end 
    end 
end 

任何想法如何解決這個問題?

回答

4

在RSpec中,水豚方法只能在it塊內部使用。您只需將您的代碼重構爲如下形式:

describe "the navigation bar" do 
    it { should have_selector('.navbar') } 

    it "should have links" do 
    within ".navbar" do 
     should have_link('Game Contest Server', href: root_path) 
     should have_link('Users', href: users_path) 
     should have_link('Sign Up', href: signup_path) 
    end 
    end 
end 
相關問題