【问题标题】:RSpec: should have_selector failed, but title is presentRSpec:应该有_选择器失败,但标题存在
【发布时间】:2012-05-02 03:19:52
【问题描述】:

我已经谷歌并阅读了大约 4 小时的 stackoverflow,但无法理解,为什么类似于 rails 教程的测试失败。

测试很简单:


# encoding: utf-8

require 'spec_helper'

describe "StaticPages" do
  subject { page }

  describe "Contacts page" do
    before { visit contacts_path }

    it { should have_selector(:title, text: "|") }
  end
end

所以,我添加了 save_and_open_page 来看看我的联系人页面有什么问题,但它看起来不错。存在标题标签并包含“|”。

我不明白,怎么了。我已经:

  • 尝试获取而不是访问;
  • 改为响应页面;
  • 在正文中永久检查其他内容(body.should have_selector(:h1))。

我还试图了解如何将 rspec 输出提高到更多信息。现在我看到了:

Failure/Error: it { should have_selector(:title, text: "|") }
       expected id :title with text "|" to return something

【问题讨论】:

  • 没有人问我怎么做。我问为什么 have_selector 不起作用。我发现,如果我使用字符串“title”而不是 symbol :title - 我的示例工作正常。

标签: ruby-on-rails rspec


【解决方案1】:

据我所知,have_selector 是 Capybara 匹配器,根据 tutorial 作为第一个参数,您可以在引号或 xpath 表示法中使用 html 元素名称,但不能使用符号。

【讨论】:

  • 谢谢,大约 4 小时前我自己把它弄清楚了 :) 但是,这是正确的答案,我标记了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多