【问题标题】:Error when writing RSpec Test Capybara::ElementNotFound: Unable to find field编写 RSpec 测试 Capybara::ElementNotFound 时出错:无法找到字段
【发布时间】:2017-02-23 22:02:49
【问题描述】:

我正在为视图编写 RSpec 测试,并在尝试填写文本字段时收到以下错误。

1) coordinators/new.html.erb populate page name text entry field filled checks for presence of filled in text field
 Failure/Error: fill_in "coordinator_name", with: 'JoeJoe'

 Capybara::ElementNotFound:
   Unable to find field "coordinator_name"
 # ./spec/views/coordinators/new.html.erb_spec.rb:47:in `block (4 levels) in <top (required)>'

这是我的代码的一部分,包括前面找到“coordinator_name”字段的测试。我很困惑为什么在第二次测试中没有找到它?

describe 'name text entry field' do
  it 'checks for presence of empty text field for name' do
   expect(rendered).to have_field('coordinator_name', text: nil)
  end
end

describe 'name text entry field filled' do
  it 'checks for presence of filled in text field' do
    fill_in "coordinator_name", with: 'JoeJoe'
    expect(rendered).to have_field('coordinator_name', text: 'JoeJoe')
  end
end

关于如何找到解决方案的任何建议?

【问题讨论】:

  • 您的测试代码显示您将 :text 选项传递给 has_field -- 如果您尝试测试的值,have_field 不采用 :text 选项它采用 :with 选项字段,又是什么rendered? - 没关系,我知道你做错了什么。
  • @KevinEtore 这是一个空文件(没有 html)。但是,当我将 save_and_open_page 用于“检查名称是否存在空文本字段”测试时,也会发生同样的事情(一个空页面)

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


【解决方案1】:

这里的问题是您正在编写视图测试 - 视图测试可以检查页面上的内容,但不能与之交互。在正常设置中,capybara 匹配器将包含在您的视图规范中,但 Capybara::DSL 不会 - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rspec.rb#L10 - 所以 fill_in 甚至不应该在视图规范中可用。我假设您已经通过在所有规范中包含 Capybara::DSL 来覆盖它?

要使您的第二个规范起作用,您需要编写一个功能规范。

【讨论】:

  • 我确实在我的 spec/rails_helper.rb 文件中包含了 Capybara::DSL
  • @Joseph Right - 其中包含了每个规范类型 - 这是不正确的 - 只是 require 'capybara/spec' 并且 DSL 只会包含在功能测试中 - 这是您可以实际与页面交互的地方.匹配器也将包含在视图规范中 - 允许您检查字段是否存在但不能更改它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多