Capybara测试:在表单提交时向服务器返回一个空数据

时间:2017-02-15 03:30:02

标签: ruby capybara

我正在编写测试,我应该编辑用户数据并单击“提交”按钮。从页面的屏幕截图可以清楚地看出,通过函数fill_in可以正确填充每个字段。但是当提交按钮单击服务器获取一个空对象时,我的测试结果会出现以下错误:

Failure/Error: Unable to find matching line from backtrace
     ActiveRecord::RecordNotFound:
       Couldn't find User with 'id'=
     # /var/lib/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/core.rb:155:in `find'
     # ./app/controllers/user/registrations_controller.rb:9:in `update_resource'
     # /var/lib/gems/2.3.0/gems/devise-4.2.0/app/controllers/devise/registrations_controller.rb:48:in `update'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/abstract_controller/base.rb:198:in `process_action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal/rendering.rb:10:in `process_action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:117:in `call'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:505:in `call'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:81:in `run_callbacks'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/abstract_controller/callbacks.rb:19:in `process_action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal/rescue.rb:29:in `process_action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/notifications.rb:164:in `block in instrument'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/notifications.rb:164:in `instrument'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
     # /var/lib/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/abstract_controller/base.rb:137:in `process'
     # /var/lib/gems/2.3.0/gems/actionview-4.2.5/lib/action_view/rendering.rb:30:in `process'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal.rb:196:in `dispatch'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_controller/metal.rb:237:in `block in action'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:45:in `serve'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/mapper.rb:49:in `serve'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/journey/router.rb:43:in `block in serve'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/journey/router.rb:30:in `each'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/journey/router.rb:30:in `serve'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:817:in `call'
     # /var/lib/gems/2.3.0/gems/warden-1.2.7/lib/warden/manager.rb:36:in `block in call'
     # /var/lib/gems/2.3.0/gems/warden-1.2.7/lib/warden/manager.rb:35:in `catch'
     # /var/lib/gems/2.3.0/gems/warden-1.2.7/lib/warden/manager.rb:35:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/etag.rb:24:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/conditionalget.rb:38:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/head.rb:13:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/flash.rb:260:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:225:in `context'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:220:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/cookies.rb:560:in `call'
     # /var/lib/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/query_cache.rb:36:in `call'
     # /var/lib/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:81:in `run_callbacks'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
     # /var/lib/gems/2.3.0/gems/railties-4.2.5/lib/rails/rack/logger.rb:38:in `call_app'
     # /var/lib/gems/2.3.0/gems/railties-4.2.5/lib/rails/rack/logger.rb:20:in `block in call'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/tagged_logging.rb:68:in `block in tagged'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/tagged_logging.rb:26:in `tagged'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/tagged_logging.rb:68:in `tagged'
     # /var/lib/gems/2.3.0/gems/railties-4.2.5/lib/rails/rack/logger.rb:20:in `call'
     # /var/lib/gems/2.3.0/gems/request_store-1.3.2/lib/request_store/middleware.rb:9:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/request_id.rb:21:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/methodoverride.rb:22:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/runtime.rb:18:in `call'
     # /var/lib/gems/2.3.0/gems/activesupport-4.2.5/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/lock.rb:17:in `call'
     # /var/lib/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/static.rb:116:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/sendfile.rb:113:in `call'
     # /var/lib/gems/2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:518:in `call'
     # /var/lib/gems/2.3.0/gems/railties-4.2.5/lib/rails/application.rb:165:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/urlmap.rb:66:in `block in call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/urlmap.rb:50:in `each'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/urlmap.rb:50:in `call'
     # /var/lib/gems/2.3.0/gems/capybara-2.12.0/lib/capybara/server.rb:43:in `call'
     # /var/lib/gems/2.3.0/gems/rack-1.6.5/lib/rack/handler/webrick.rb:88:in `service'

我的测试场景是:

before  :each do
      visit '/login'
      wait_for_ajax
      fill_in 'email', with: 'admin@hostel.com'
      fill_in 'password', with: 'testtest'
      click_button 'submit'
      wait_for_ajax
      wait_url '/dashboard'
      visit '/users/1'
      wait_for_ajax
    end
    scenario 'click on submit with edition' do
                find('#name').set('EDIT')
                fill_in 'email', with: 'testtest@hostel.com'
                click_button 'パスワードを変更する'
                fill_in 'password', with: 'testtest'
                fill_in 'password-confirmation', with: 'testtest'
                click_button '保存'
                page.save_screenshot('po.png')
                expect(page).to have_current_path('/users')
    end

HTML =>

<div class="new_room_and_build">
      <div name="form_user" class="form-horizontal">
        <div class="editing_user_form">
          <!---->
          <div class="form-group"><label for="name" class="col-sm-2 control-label">名前</label>
            <div class="col-sm-3"><input id="name" type="text" class="form-control"></div>
          </div>
          <div class="form-group"><label for="email" class="col-sm-2 control-label">メール</label>
            <div class="col-sm-3"><input id="email" type="text" class="form-control"></div>
          </div>
          <!---->
          <div class="col-sm-2"></div>
          <div class="col-sm-10"><button class="hvr-fade btn btn-link but_edit_pass">パスワードを変更する</button></div>
          <div class="edit-password">
            <div class="form-group"><label for="password" class="col-sm-2 control-label">パスワード</label>
              <div class="col-sm-3"><input id="password" name="password" size="40" type="password" class="form-control"></div>
            </div>
            <div class="col-sm-2 control-label"></div>
            <div class="pass_hint col-sm-10">パスワードの文字数は6文字以上にしてください</div>
            <div class="form-group"><label for="password-confirmation" class="col-sm-2 control-label">パスワード確認</label>
              <div class="col-sm-3"><input id="password-confirmation" name="password-confirmation" size="40" type="password" class="form-control"></div>
            </div>
          </div>
        </div>
        <div class="form-group float_right col-sm-4"><a href="/users" class="router-link-active"><button id="cancel" type="submit" class="btn but_add hvr-fade float_right"><i aria-hidden="true" class="fa fa-times"></i> キャンセル</button></a>
          <button id="submit" v="" class="btn but_add hvr-fade float_right"
            disabled=""><i aria-hidden="true" class="fa fa-check"></i> 保存</button>
        </div>
      </div>
    </div>
  </div>

应在提交点击时调用的服务器端方法:

def update_resource(resource, params)
      authorize! :update, User
      puts params.inspect
      resource = User.find(params[:id])
      params[:role] = Role.find(params[:role])
      if params.key? :password
        params[:password_expiry] = Time.now.to_i + 86400
        resource.update_attributes(params)
        UserMailer.send_user_info(@current_user.name, resource.email, params[:password]).deliver_later
      else
        resource.update_without_password(params)
      end
    end

我非常感谢任何帮助或任何建议

0 个答案:

没有答案