gpt4 book ai didi

ruby-on-rails - rspec 测试结果 Capybara::ElementNotFound

转载 作者:行者123 更新时间:2023-11-28 21:32:03 26 4
gpt4 key购买 nike

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

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

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

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

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

表单/ View (部分):

 <%= 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 中。这也无济于事。

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

最佳答案

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

before { visit new_my_model_path }

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

关于ruby-on-rails - rspec 测试结果 Capybara::ElementNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16813827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com