写入 RSpec 测试时出错 Capybara::ElementNotFound:无法找到字段

Error when writing RSpec Test Capybara::ElementNotFound: Unable to find field

我正在编写 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

关于如何找到解决方案有什么建议吗?

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

要使第二个规范生效,您需要编写功能规范。