2014-12-30 23 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