Capybara :: ElementNotFound,但是在那里 [英] Capybara::ElementNotFound, but it is there
问题描述
我遇到以下错误:
Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
规格:
require 'spec_helper'
describe SessionsController do
before :each do
@user = FactoryGirl.create(:user)
end
context 'creating a new session' do
it 'can set the current_user variable to the logged user' do
visit '/login'
fill_in 'username', with: 'gabrielhilal' #I have tried `Username` as well
fill_in 'password', with: 'secret'
click_button 'Login'
current_user.should == @user
current_user.username.should == 'gabrielhilal'
end
end
context 'destroying existing session' do
xit 'can destroy the current_user' do
end
end
end
但是我的表单中有字段username
:
But I have the field username
in my form:
<form accept-charset="UTF-8" action="/sessions" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" / <input name="authenticity_token" type="hidden" value="x7ORLDIvq1BXr8SOkd/Zla9Pl5R5tBXAtyflCpTGCtY=" /></div>
<div class="field">
<label for="username">Username</label>
<input id="username" name="username" type="text" />
</div>
<div class="field">
<label for="password">Password</label>
<input id="password" name="password" type="password" />
</div>
<div class="actions">
<input name="commit" type="submit" value="Login" />
</div>
</form>
我对cucumber
做了类似的测试,并且通过了测试,该测试确认了字段username
在那里:
I did a similar test with cucumber
and it is passing, which confirms that the field username
is there: When to switch from cucumber to rspec in the BDD cycle for a login procedure
有什么主意吗?
编辑-我添加了save_and_open_page
,这给了我一个空白页. puts "#{page.html.inspect}"
还会返回经验.
EDIT - I have added the save_and_open_page
, which gives me a blank page. The puts "#{page.html.inspect}"
also returns empity.
""
Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
推荐答案
我知道您已经回答了自己的问题(有点),但是我仍然有必要让您知道您提供的示例是控制器和请求规范的组合...
I know you've already answered your own question (kind of), but I feel it's still warranted to let you know that the example you've provided is a mixture of a controller and a request spec...
引用 RSpec-Controller Specs 的逐字记录:
Note: To encourage more isolated testing, views are not rendered by default in controller specs.
主要区别(RSpec解释它是哪种规格的方式)是一行:describe SessionsController do
.
The main difference (in the way RSpec interprets what kind of spec it is) is the line: describe SessionsController do
.
使其与RSpec和测试最佳实践"保持一致:
To align it with RSpec and Testing best practices:
- 将规格移至
spec/requests
- 将描述更改为
describe "SessionsController" do
- 删除
render_views
- Move the spec to
spec/requests
- Change the describe to
describe "SessionsController" do
- Remove
render_views
您的规范应该可以正常运行...
your spec should run just fine...
查看 RSpec-请求规格,了解我的意思.我还将查看 betterspecs.org ,了解您可以改进测试的方式.
Check out RSpec - Request Specs for what I mean. I would also check out betterspecs.org for ways you can improve on testing.
这篇关于Capybara :: ElementNotFound,但是在那里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!