【问题标题】:rspec Test Result Capybara::ElementNotFoundrspec 测试结果 Capybara::ElementNotFound
【发布时间】:2013-05-29 12:24:25
【问题描述】:

有人请我帮忙开发 Rails 应用程序。我正在经历一些失败的 rpsec 测试并让它们通过。我有一个我正在用头撞墙。我们使用的是 Rails 3.2.13 和 Ruby 2.0 注意:我完全是 rspec 的一个 nube。

我有一个 id 为 vet-as-faculty 的表格。测试正在寻找这个而不是找到它。我得到了错误:

Capybara::ElementNotFound:
   Unable to find css "form#vet-as-faculty

我在测试中的其他表单元素上也遇到了类似的错误,但测试在此时或此时失败。

我知道这“通常”意味着表单上的标记不正确或测试中的某些内容拼写错误(我发现了其中一些)。但是这个我就是拿不到。我在谷歌上搜索了这个错误并找到了一些东西,但似乎没有任何东西可以帮助我解决这个问题。我希望你们那里的大师会看到一些对我来说很简单的东西。

表单/视图(部分):

 <%= form_for @profile, :url => { :action => :vet_as_faculty }, :html => { :id => "vet-as-faculty" } do |f| %>
  <fieldset class="row1"><legend>Vet Me As Faculty</legend>
    <div class="form-pair" id="faculty-url">
    <div class="form-item">
      <label>Faculty URL</label>
    </div>
  <fieldset class="row2">
    <div class="form-submit" id="save-faculty">
      <%= f.submit "Send for Review" %>
    </div>
  </fieldset>
<% end %>

查看源代码显示为:

<form accept-charset="UTF-8" action="/research/chr/casestudies/profile/vet_as_faculty" class="edit_user" id="vet-as-faculty" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="FZtjli8HaGD3koUvSFAYR2Gy+waL8KDWflMNUo4tXXw=" /></div>
  <fieldset class="row1"><legend>Vet Me As Faculty</legend>
    <div class="form-pair" id="faculty-url">
      <div class="form-item">
        <label>Faculty URL</label>
      </div>
      <div class="form-value">
        <input id="user_faculty_url" name="user[faculty_url]" size="30" type="text" />
      </div>
  </fieldset>

  <fieldset class="row2">
    <div class="form-submit" id="save-faculty">
      <input name="commit" type="submit" value="Send for Review" />
    </div>
  </fieldset>
...

所以,我将 for id 视为 'id="vet-as-faculty"'。但是,测试没有看到这一点。

测试代码:

context "within the faculty vetting block" do
    it "has the necessary form fields" do
      within "form#vet-as-faculty" do 
        expect(page).to have_css("input#user_faculty_url")
        expect(page).to have_css("input#save-faculty")
      end
    end
 end

这一测试的错误输出:

3) Edit Profile View within the faculty vetting block has the necessary form fields
   Failure/Error: within "form#vet-as-faculty" do
   Capybara::ElementNotFound:
     Unable to find css "form#vet-as-faculty"
   # ./spec/features/edit_profile_view_spec.rb:48:in `block (3 levels) in <top (required)>'

我尝试在测试中添加更多向下钻取类型的 css

within "#page #content form#vet-as-faculty" do

表单在 div#content 内,内容在 div#page 内。这也无济于事。

有什么想法吗?感谢您阅读本文。

【问题讨论】:

  • 你确定你在你认为的页面上吗?您与我们共享的测试中没有访问方法调用。
  • 您是否尝试过前面的within 块?您可以在 it 块中说明您的期望(以便规范扫描整个页面),看看是否有帮助。 Capybara 的功能规格可能非常善变。祝你好运!
  • 感谢您的回复。也许代码没有看到视图页面。如前所述,我对 rspec 完全陌生。我看到一个带有“背景”的块。其中有一行:访问edit_profile_path(@user)。我不知道这是否是你所指的。我将与编写此代码的程序员联系并问他。谢谢
  • aceofbassgreg:是的,我尝试注释掉 inside 块。教师网址 ID 和提交按钮 ID 显示相同的错误。

标签: ruby-on-rails testing rspec


【解决方案1】:

最好的猜测是您实际上并没有导航到您的表单所在的页面

before { visit new_my_model_path }

作为更一般的建议。避免编写只断言标记/css 的测试。专注于您需要此表单完成的行为,您的测试将更有价值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多