加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Capybara :: ElementNotFound,但它就在那里

发布时间:2020-12-17 01:47:25 所属栏目:百科 来源:网络整理
导读:我收到以下错误: 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
我收到以下错误:

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

但是我的表单中有字段用户名:

<form accept-charset="UTF-8" action="/sessions" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" / <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>

我用黄瓜进行了类似的测试,它正在传递,这证实了字段用户名是:When to switch from cucumber to rspec in the BDD cycle for a login procedure

任何的想法?

编辑 – 我添加了save_and_open_page,它给了我一个空白页面.投注“#{page.html.inspect}”也会返回empity.

""

Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:12:in `block (3 levels) in <top (required)>'

解决方法

我知道你已经回答了你自己的问题(有点),但我觉得有必要告诉你,你所提供的例子是控制器和请求规范的混合……

从RSpec – Controller Specs逐字引用:
注意:为了鼓励进行更多隔离测试,默认情况下不会在控制器规范中呈现视图.

主要区别(在RSpec解释它是什么样的规范的方式)是行:描述SessionsController做.

要使其与RSpec和测试最佳实践保持一致:

>将规范移至spec / requests
>更改描述以描述“SessionsController”
>删除render_views

你的规格应该运行得很好……

查看RSpec – Request Specs我的意思.我还会查看betterspecs.org,了解如何改进测试.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读