Capybara :: ElementNotFound,但它就在那里
我收到以下错误:
Capybara::ElementNotFound: Unable to find field "username" ./spec/controllers/sessions_controller_spec.rb:10:in `block (3 levels) in '
规格:
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
:
我用cucumber
进行了类似的测试并且它正在传递,这确认了字段username
在那里: 何时在BDD周期中从黄瓜切换到rspec以进行登录程序
任何的想法?
编辑 – 我添加了save_and_open_page
,它给了我一个空白页面。 投注puts "#{page.html.inspect}"
也会返回empity。
"" Capybara::ElementNotFound: Unable to find field "username" ./spec/controllers/sessions_controller_spec.rb:12:in `block (3 levels) in '
我知道你已经回答了你自己的问题(有点),但我觉得有必要让你知道你提供的例子是控制器和请求规范的混合…
从RSpec中逐字引用- 控制器规格 : Note: To encourage more isolated testing, views are not rendered by default in controller specs.
主要区别(在RSpec解释它是什么样的规范的方式)是行: describe SessionsController do
。
要使其与RSpec和测试最佳实践保持一致:
- 将规范移至
spec/requests
- 更改描述以
describe "SessionsController" do
- 删除
render_views
你的规格应该运行得很好……
查看RSpec – 请求规格我的意思。 我还会查看betterspecs.org以了解可以改进测试的方法。
您可以使用终端调试它,例如:
it {puts page.body}
并查看是否有“用户名”输入。 但我认为第一步的问题是visit '/login'
我意识到我必须明确告诉我的控制器渲染视图。 我在rspec自述文件中找到了它,所以我解决了添加render_views
的问题。
describe SessionsController do render_views #this line solved the problem before :each do user = FactoryGirl.create(:user) end ...