have_selector 测试失败

Muk*_*215 4 rspec ruby-on-rails ruby-on-rails-4

我遇到了问题,包括 rspec 的 should have_selector 问题:

这是我的代码:

describe "GET 'home'" do
  it "returns http success" do
    get 'home'
    expect(response).to be_success
  end

  it "should have the right title" do
    should have_selector("title", 
          :content => "Ruby on Rails Tutorial Sample App | Home")
  end
end
Run Code Online (Sandbox Code Playgroud)

我在顶部添加了以下内容:

RSpec.describe PagesController, :type => :controller do
  render_views
Run Code Online (Sandbox Code Playgroud)

我的html5有以下内容:

<title>Ruby on Rails Tutorial Sample App | Home</title>
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息:

失败:

1) PagesController GET 'home' should have the right title
 Failure/Error: should have_selector("title",
   expected #<PagesController:0x007fceef586a90> to respond to `has_selector?`
 # ./spec/controllers/pages_controller_spec.rb:14:in `block (3 levels) in <top  (required)>
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

rspec -v 3.0.2

导轨 4.1.1

先感谢您。

Fre*_*ung 5

默认情况下,Rspec 3 控制器规范中不包含水豚匹配器。您可以通过执行以下操作来更改单个规范

include Capybara::RSpecMatchers
Run Code Online (Sandbox Code Playgroud)

或者,在你的规范助手中

  config.include Capybara::RSpecMatchers, :type => :controller
Run Code Online (Sandbox Code Playgroud)

您的下一个问题是,最新版本的水豚默认不允许您测试不可见元素的存在,并且 title 元素被认为是不可见的。您应该改用have_title匹配器。