Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 带有Capybara 2.4和rspec 3以及rails 4的测试页面标题_Ruby On Rails_Ruby On Rails 4_Rspec_Capybara - Fatal编程技术网

Ruby on rails 带有Capybara 2.4和rspec 3以及rails 4的测试页面标题

Ruby on rails 带有Capybara 2.4和rspec 3以及rails 4的测试页面标题,ruby-on-rails,ruby-on-rails-4,rspec,capybara,Ruby On Rails,Ruby On Rails 4,Rspec,Capybara,我正在尝试在rails中测试静态页面的标题。我用的是水豚2.4.4和rspec3 我的测试如下所示 静态页面\u控制器\u规范rb require 'spec_helper' require 'rails_helper' describe StaticPagesController do describe "GET 'Index'" do it "should be successful" do visit root_path response.should

我正在尝试在rails中测试静态页面的标题。我用的是水豚2.4.4和rspec3

我的测试如下所示 静态页面\u控制器\u规范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

我也花了好几个小时想弄明白

最终起作用的是这个

将水豚安装为宝石

group :test do
  gem 'rspec'
  gem 'capybara'
end
然后运行
bundle安装

将以下内容添加到spec/spec_helper.rb的顶部

require 'capybara/rails'
require 'capybara/rspec'
然后在RSpec.configure中,添加
config.include Capybara::DSL

RSpec.configure do |config|
  .
  .
  .
  config.include Capybara::DSL
end
然后在您的
页面\u controller\u spec.rb
中,按如下方式修改以使用

注意,我已经包含了
Capybara.ignore\u hidden\u 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
如果你还想看别的

下面的备忘单也应该很方便

您应该期望第页(第页)。有内容而不是发布查看代码?这应该是您的答案。@trueinViso我尝试了最新的答案,并收到预期的“”将包含您尝试的“索引”错误
expect(第页)。要使用标题“索引”
?您正在测试的页面上有
索引
?为了确保您在测试中执行了
保存和打开页面
,并检查了元素以确保html存在?我很抱歉没有早点回来。我只是尝试实施您的解决方案,但它仍然不起作用。我收到带有文本“title”的“期望找到xpath”//title”,但没有匹配项。