2014-12-30 53 views
1

我想在rails中測試我的靜態頁面的標題。我使用的是水豚2.4.4和RSpec 3.測試頁面標題與水豚2.4和rspec 3和rails 4

我的測試看起來如下 static_pages_controller_spec.rb

require 'spec_helper' 
require 'rails_helper' 
describe StaticPagesController do 
    describe "GET 'Index'" do 
    it "should be successful" do 
     visit root_path 
     response.should be_success 
    end 

    it "should have the right title" do 
     visit root_path 
     expect(page).to have_selector('title', 
        :text => "Index", 
        :visible => false) 
    end 
    end 
end 

頁面確實有正確的標題集。 我得到的錯誤說下面

Failure/Error: expect(page).to have_selector('title', 
     expected to find css "title" with text "Index" but there were no matches 
+0

你應該期望頁面(頁面)。以have_content代替 – Joel

+0

也許發佈視圖代碼? – roob

+0

http://stackoverflow.com/questions/13573525/rspec-capybara-2-0-tripping-up-my-have-selector-tests 這應該有你的答案。 – trueinViso

回答

0

我花了好幾個小時,試圖弄明白爲好。

最終什麼工作是這樣的

安裝水豚作爲寶石

group :test do 
    gem 'rspec' 
    gem 'capybara' 
end 

和運行bundle install

添加以下規格的頂部/ spec_helper.rb

require 'capybara/rails' 
require 'capybara/rspec' 

然後在RSpec.configure裏面添加config.include Capybara::DSL

RSpec.configure do |config| 
    . 
    . 
    . 
    config.include Capybara::DSL 
end 

然後在你的pages_controller_spec.rb,修改使用如下。

注意,我已經包括Capybara.ignore_hidden_elements = false

describe PagesController do 
    render_views 
    Capybara.ignore_hidden_elements = false 

    describe "GET 'home'" do 
    it "should be successful" do 
     get 'home' 
     response.should be_success 
    end 
    it "should have the right title" do 
     get 'home' 
     response.body.should have_xpath("//title",:text => "ROR") 
    end 
    end 

end 

Check my repository如果你想看到別的

以下的cheatsheet應該來得心應手以及
https://gist.github.com/them0nk/2166525

+0

我很抱歉沒有及時回覆。我只是試圖實現你的解決方案,但它仍然無法正常工作。我收到「預計會找到xpath」//標題「帶有文本」TITLE「,但沒有匹配」。 – Questifer

相關問題