Lor*_*ein 5 rspec ruby-on-rails capybara
在我将 Capybara 从 2.1.0 升级到 2.2.1 并将 Poltergeist 从 1.4.1 升级到 1.5.0 之后,以下 rspec 期望在我的 rails 应用程序的测试中开始失败:
it{ expect(page).to have_field("foo_field_id", with: nil) }
Run Code Online (Sandbox Code Playgroud)
rspec 错误如下所示:
Failure/Error: it{ expect(page).to have_field("foo_field_id", with: nil) }
Capybara::ExpectationNotMet:
expected to find field "foo_field_id" but there were no matches. Also found "", which matched the selector but not all filters.
Run Code Online (Sandbox Code Playgroud)
如果我抛出一个断点进行检查,则该值确实为零:
» page.find_field('foo_field_id').value
=> nil
Run Code Online (Sandbox Code Playgroud)
如果我将“with”更改为“text”,则断言通过:
it{ expect(page).to have_field("foo_field_id", text: nil) }
Run Code Online (Sandbox Code Playgroud)
HTML 表单字段如下所示:
<input class="string optional" id="foo_field_id" name="foo[field_id]" type="text">
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
根据https://github.com/jnicklas/capybara/pull/1169,这是因为最近的变化:
不再可能与 nil 匹配,因为 to_s 会在您传递给的内容上被调用。
解决方法是执行以下操作:
expect(find_field("foo_field_id").text).to be_blank
Run Code Online (Sandbox Code Playgroud)