【发布时间】:2013-08-13 09:19:18
【问题描述】:
我收到以下错误:
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 字段:
<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>
我对@987654326@ 进行了类似的测试,它通过了,这证实了username 字段存在:When to switch from cucumber to rspec in the BDD cycle for a login procedure
有什么想法吗?
编辑 - 我添加了save_and_open_page,它给了我一个空白页。 puts "#{page.html.inspect}" 也返回空。
""
Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
【问题讨论】:
-
尝试使用 save_and_open_page 和启动 gem
-
谢谢@hypee...我添加了
save_and_open_page,但由于某种原因我的页面是空白的。
标签: ruby-on-rails rspec capybara bdd ruby-on-rails-4