rspec 测试结果 Capybara::ElementNotFound [英] rspec Test Result Capybara::ElementNotFound

查看:51
本文介绍了rspec 测试结果 Capybara::ElementNotFound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人要求我帮助开发 Rails 应用.我正在经历一些失败的 rpsec 测试并让它们通过.我有一个我正在用头撞墙.我们使用的是 Rails 3.2.13 和 Ruby 2.0 注意:我对 rspec 完全不了解.

我有一个 ID 为 vet-as-faculty 的表格.测试正在寻找这个而不是找到它.我收到错误:

水豚::ElementNotFound:无法找到 cssform#vet-as-faculty

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

我知道这通常"意味着表单上的标记不正确或测试中的某些内容拼写错误(我发现了其中的一些).但这个我就是拿不到.我在谷歌上搜索了这个错误并找到了一些东西,但似乎没有什么对我有帮助.我希望你们的大师们能看到一些对我来说很简单的东西.

表单/视图(部分):

 <%= form_for @profile, :url =>{:动作=>:vet_as_faculty }, :html =>{ :id =>兽医师"} 做 |f|%><fieldset class="row1"><legend>Vet Me as Faculty</legend><div class="form-pair" id="faculty-url"><div class="form-item"><label>教师网址</label>

<fieldset class="row2"><div class="form-submit" id="save-faculty"><%= f.submit 送审" %>

</fieldset><%结束%>

查看源代码显示为:

<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>教师网址</label>

<div class="form-value"><input id="user_faculty_url" name="user[faculty_url]" size="30" type="text"/>

</fieldset><fieldset class="row2"><div class="form-submit" id="save-faculty"><input name="commit" type="submit" value="发送审核"/>

</fieldset>...

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

测试代码:

上下文在教员审查块内"做它有必要的表单域"做在form#vet-as-faculty"中做期望(页面).to have_css("input#user_faculty_url")期望(页面).to have_css("input#save-faculty")结尾结尾结尾

这一测试的错误输出:

3) 教师审查块中的编辑个人资料视图具有必要的表单字段失败/错误:在form#vet-as-faculty"中做水豚::ElementNotFound:无法找到 cssform#vet-as-faculty"# ./spec/features/edit_profile_view_spec.rb:48:in `block (3 levels) in <top (required)>'

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

在#page #content form#vet-as-faculty"中做

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

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

解决方案

最好的猜测是您还没有真正导航到表单所在的页面

before { 访问 new_my_model_path }

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

I've been asked to help out on a Rails app. I'm going through some failing rpsec tests and getting them to pass. I've got one on which I'm banging my head against a wall. We are using Rails 3.2.13 and Ruby 2.0 Note: I'm totally a nube on rspec.

I have a form with an id of vet-as-faculty. The test is looking for this and not finding it. I get the error:

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

I get a similar error on other form elements in the test also, but the test is failing on or at this point.

I know this "usually" means the markup on the form is not correct or something in the test is misspelled (I've found a few of those). But this one I just cannot get. I've Googled on this error and found a bit of stuff, but nothing that seems to help me on this one. I'm hoping you gurus out there will see something simple for me.

Form/view (in part):

 <%= 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 %>

View source shows this as:

<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>
...

So, I see the for id as 'id="vet-as-faculty"'. but, the test does not see this.

The Test Code:

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

The error output for this one test:

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)>'

I've tried to add more drill down type css in the test

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

The form is inside div#content and content is inside div#page. This does not help either.

Any thoughts? Thanks for reading this.

解决方案

Best guess is that you haven't actually navigated to the page where your form is

before { visit new_my_model_path }

As a more general piece of advice. Avoid writing tests that just assert markup/css. Focus on the behavior you need this form to accomplish instead and your tests will be a lot more valuable.

这篇关于rspec 测试结果 Capybara::ElementNotFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆