【问题标题】:have_selector is failing testhave_selector 测试失败
【发布时间】:2014-06-22 07:14:37
【问题描述】:

我遇到了一个问题,包括 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

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

RSpec.describe PagesController, :type => :controller do
  render_views

我的html5有以下内容:

<title>Ruby on Rails Tutorial Sample App | Home</title>

我收到一条错误消息:

失败:

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)>

有人可以帮忙吗?

rspec -v 3.0.2

导轨 4.1.1

提前谢谢你。

【问题讨论】:

  • 如果删除, :type =&gt; :controller会怎样?我认为检查页面内容不是控制器规范的责任。

标签: ruby-on-rails ruby-on-rails-4 rspec


【解决方案1】:

默认情况下,Rspec 3 在控制器规范中不包含 capybara 匹配器。您可以通过以下方式为单个规范更改此设置

include Capybara::RSpecMatchers

或者,在您的规范助手中

  config.include Capybara::RSpecMatchers, :type => :controller

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

【讨论】:

【解决方案2】:

你好红宝石爱好者!

我遇到了这个问题,那是因为两件事,首先我没有使用 capybara gem,第二个 have_selector 只接受 :count, :minimum, :maximum, :between, :text, :visible, :exact 之一, :match, :wait 键和不理解 :content。

我相信你已经解决了这个问题,但是对于那些最近开始学习 Ruby on Rails 并遇到这样的问题的人,我应该说先消除它,首先将 capybara gem 放入你的 Gemfile 中,如下所示:

group :development, :test do
  ...
  gem 'capybara'
end

并运行命令

bundle install

然后测试你的视图是否有一个特定的标题,在 pages_controller_spec.rb 文件中写入

describe "GET #home" do
  ...
  it "should have the right title" do
    get :home
    expect(response.body).to have_selector('title', :text => 'Ruby on Rails rocks')
  end
end

我希望它会帮助别人。

欢呼!

【讨论】:

    【解决方案3】:

    检查内容是查看规范... 控制器规范意味着检查响应状态/路径、渲染页面/部分、实例变量过滤器参数。

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多