"没有方法错误"来自使用Capybara的Rails中的测试

时间:2015-12-15 22:19:55

标签: ruby-on-rails ruby rspec capybara

我使用RSpec和Capybara编写了一个简单的测试。

我的失败规格是 sign_in_feature_spec.rb:

require 'spec_helper'

describe 'the signin process', :type => :feature do
  it 'signs me in' do
    admin_user = FactoryGirl.create(:admin_user)
    login_as(admin_user, :scope => :admin_user)
    visit root_path
    puts page.body
  end
end

当我运行此规范时,我会看到下一个报告:

  

1例,0次失败,1次传递

所以,测试成功了,但我从puts page.body得到了这个输出:

NoMethodError at /
==================

> undefined method `saved_filters' for #<AdminUser:0x00000009abf870>

activemodel (4.1.9) lib/active_model/attribute_methods.rb, line 435
-------------------------------------------------------------------

``` ruby
  430       def method_missing(method, *args, &block)
  431         if respond_to_without_attributes?(method, true)
  432           super
  433         else
  434           match = match_attribute_method?(method.to_s)
> 435           match ? attribute_missing(match, *args, &block) : super
  436         end
  437       end
  438   
  439       # +attribute_missing+ is like +method_missing+, but for attributes. When
  440       # +method_missing+ is called we check to see if there is a matching
```

App backtrace
-------------

 - activemodel (4.1.9) lib/active_model/attribute_methods.rb:435:in `method_missing'
 - activerecord (4.1.9) lib/active_record/attribute_methods.rb:213:in `method_missing'
 - lib/active_admin/filter_saver/controller.rb:35:in `save_filters?'
 - config/initializers/active_admin.rb:165:in `block (2 levels) in <top (required)>'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:438:in `block in make_lambda'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:143:in `block in halting_and_conditional'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:229:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:86:in `run_callbacks'
 - actionpack (4.1.9) lib/abstract_controller/callbacks.rb:19:in `process_action'
 - actionpack (4.1.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
 - config/initializers/instrumentation_notification.rb:28:in `block in process_action'
 - activesupport (4.1.9) lib/active_support/notifications.rb:159:in `block in instrument'
 - activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 - activesupport (4.1.9) lib/active_support/notifications.rb:159:in `instrument'
 - config/initializers/instrumentation_notification.rb:27:in `process_action'
 - actionpack (4.1.9) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
 - activerecord (4.1.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 - actionpack (4.1.9) lib/abstract_controller/base.rb:136:in `process'
 - actionview (4.1.9) lib/action_view/rendering.rb:30:in `process'
 - actionpack (4.1.9) lib/action_controller/metal.rb:196:in `dispatch'
 - actionpack (4.1.9) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
 - actionpack (4.1.9) lib/action_controller/metal.rb:232:in `block in action'
 - actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
 - actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:50:in `call'
 - actionpack (4.1.9) lib/action_dispatch/journey/router.rb:73:in `block in call'
 - actionpack (4.1.9) lib/action_dispatch/journey/router.rb:59:in `call'
 - actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:685:in `call'
 - warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
 - warden (1.2.3) lib/warden/manager.rb:34:in `call'
 - rack (1.5.5) lib/rack/etag.rb:23:in `call'
 - rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
 - rack (1.5.5) lib/rack/head.rb:11:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/flash.rb:254:in `call'
 - rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
 - rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/cookies.rb:562:in `call'
 - activerecord (4.1.9) lib/active_record/query_cache.rb:36:in `call'
 - activerecord (4.1.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:82:in `run_callbacks'
 - actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 - railties (4.1.9) lib/rails/rack/logger.rb:38:in `call_app'
 - railties (4.1.9) lib/rails/rack/logger.rb:20:in `block in call'
 - activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `block in tagged'
 - activesupport (4.1.9) lib/active_support/tagged_logging.rb:26:in `tagged'
 - activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `tagged'
 - railties (4.1.9) lib/rails/rack/logger.rb:20:in `call'
 - request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 - rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
 - rack (1.5.5) lib/rack/runtime.rb:17:in `call'
 - activesupport (4.1.9) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/static.rb:84:in `call'
 - rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
 - railties (4.1.9) lib/rails/engine.rb:514:in `call'
 - railties (4.1.9) lib/rails/application.rb:144:in `call'
 - rack (1.5.5) lib/rack/builder.rb:138:in `call'
 - rack (1.5.5) lib/rack/urlmap.rb:65:in `block in call'
 - rack (1.5.5) lib/rack/urlmap.rb:50:in `call'
 - rack-test (0.6.3) lib/rack/mock_session.rb:30:in `request'
 - rack-test (0.6.3) lib/rack/test.rb:244:in `process_request'
 - rack-test (0.6.3) lib/rack/test.rb:58:in `get'
 - capybara (2.5.0) lib/capybara/rack_test/browser.rb:60:in `process'
 - capybara (2.5.0) lib/capybara/rack_test/browser.rb:35:in `process_and_follow_redirects'
 - capybara (2.5.0) lib/capybara/rack_test/browser.rb:21:in `visit'
 - capybara (2.5.0) lib/capybara/rack_test/driver.rb:42:in `visit'
 - capybara (2.5.0) lib/capybara/session.rb:232:in `visit'
 - capybara (2.5.0) lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
 - rspec-rails (3.2.0) lib/rspec/rails/example/feature_example_group.rb:28:in `visit'
 - spec/features/sign_in_feature_spec.rb:7:in `block (2 levels) in <top (required)>'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:177:in `block in run'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:385:in `block in with_around_and_singleton_context_hooks'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:343:in `block in with_around_example_hooks'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:474:in `block in run'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:614:in `block in run_around_example_hooks_for'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:247:in `call'
 - rspec-rails (3.2.0) lib/rspec/rails/adapters.rb:111:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:333:in `instance_exec'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:387:in `execute_with'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:247:in `call'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:617:in `run_around_example_hooks_for'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:474:in `run'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:343:in `with_around_example_hooks'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:174:in `run'
 - rspec-core (3.2.0) lib/rspec/core/example_group.rb:548:in `block in run_examples'
 - rspec-core (3.2.0) lib/rspec/core/example_group.rb:544:in `run_examples'
 - rspec-core (3.2.0) lib/rspec/core/example_group.rb:512:in `run'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:110:in `block (3 levels) in run_specs'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:110:in `block (2 levels) in run_specs'
 - rspec-core (3.2.0) lib/rspec/core/configuration.rb:1526:in `with_suite_hooks'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:109:in `block in run_specs'
 - rspec-core (3.2.0) lib/rspec/core/reporter.rb:62:in `report'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:108:in `run_specs'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:86:in `run'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:70:in `run'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:38:in `invoke'
 - rspec-core (3.2.0) exe/rspec:4:in `<top (required)>'
 - ruby/2.1.0/bin/rspec:23:in `<top (required)>'

Full backtrace
--------------

 - activemodel (4.1.9) lib/active_model/attribute_methods.rb:435:in `method_missing'
 - activerecord (4.1.9) lib/active_record/attribute_methods.rb:213:in `method_missing'
 - lib/active_admin/filter_saver/controller.rb:35:in `save_filters?'
 - config/initializers/active_admin.rb:165:in `block (2 levels) in <top (required)>'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:438:in `block in make_lambda'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:143:in `block in halting_and_conditional'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:229:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:86:in `run_callbacks'
 - actionpack (4.1.9) lib/abstract_controller/callbacks.rb:19:in `process_action'
 - actionpack (4.1.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
 - config/initializers/instrumentation_notification.rb:28:in `block in process_action'
 - activesupport (4.1.9) lib/active_support/notifications.rb:159:in `block in instrument'
 - activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 - activesupport (4.1.9) lib/active_support/notifications.rb:159:in `instrument'
 - config/initializers/instrumentation_notification.rb:27:in `process_action'
 - actionpack (4.1.9) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
 - activerecord (4.1.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 - actionpack (4.1.9) lib/abstract_controller/base.rb:136:in `process'
 - actionview (4.1.9) lib/action_view/rendering.rb:30:in `process'
 - actionpack (4.1.9) lib/action_controller/metal.rb:196:in `dispatch'
 - actionpack (4.1.9) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
 - actionpack (4.1.9) lib/action_controller/metal.rb:232:in `block in action'
 - actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
 - actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:50:in `call'
 - actionpack (4.1.9) lib/action_dispatch/journey/router.rb:73:in `block in call'
 - actionpack (4.1.9) lib/action_dispatch/journey/router.rb:59:in `call'
 - actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:685:in `call'
 - warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
 - warden (1.2.3) lib/warden/manager.rb:34:in `call'
 - rack (1.5.5) lib/rack/etag.rb:23:in `call'
 - rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
 - rack (1.5.5) lib/rack/head.rb:11:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/flash.rb:254:in `call'
 - rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
 - rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/cookies.rb:562:in `call'
 - activerecord (4.1.9) lib/active_record/query_cache.rb:36:in `call'
 - activerecord (4.1.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 - activesupport (4.1.9) lib/active_support/callbacks.rb:82:in `run_callbacks'
 - actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
 - better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 - railties (4.1.9) lib/rails/rack/logger.rb:38:in `call_app'
 - railties (4.1.9) lib/rails/rack/logger.rb:20:in `block in call'
 - activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `block in tagged'
 - activesupport (4.1.9) lib/active_support/tagged_logging.rb:26:in `tagged'
 - activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `tagged'
 - railties (4.1.9) lib/rails/rack/logger.rb:20:in `call'
 - request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 - rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
 - rack (1.5.5) lib/rack/runtime.rb:17:in `call'
 - activesupport (4.1.9) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
 - actionpack (4.1.9) lib/action_dispatch/middleware/static.rb:84:in `call'
 - rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
 - railties (4.1.9) lib/rails/engine.rb:514:in `call'
 - railties (4.1.9) lib/rails/application.rb:144:in `call'
 - rack (1.5.5) lib/rack/builder.rb:138:in `call'
 - rack (1.5.5) lib/rack/urlmap.rb:65:in `block in call'
 - rack (1.5.5) lib/rack/urlmap.rb:50:in `call'
 - rack-test (0.6.3) lib/rack/mock_session.rb:30:in `request'
 - rack-test (0.6.3) lib/rack/test.rb:244:in `process_request'
 - rack-test (0.6.3) lib/rack/test.rb:58:in `get'
 - /usr/lib/ruby/2.2.0/forwardable.rb:183:in `get'
 - capybara (2.5.0) lib/capybara/rack_test/browser.rb:60:in `process'
 - capybara (2.5.0) lib/capybara/rack_test/browser.rb:35:in `process_and_follow_redirects'
 - capybara (2.5.0) lib/capybara/rack_test/browser.rb:21:in `visit'
 - capybara (2.5.0) lib/capybara/rack_test/driver.rb:42:in `visit'
 - capybara (2.5.0) lib/capybara/session.rb:232:in `visit'
 - capybara (2.5.0) lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
 - rspec-rails (3.2.0) lib/rspec/rails/example/feature_example_group.rb:28:in `visit'
 - spec/features/sign_in_feature_spec.rb:7:in `block (2 levels) in <top (required)>'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:177:in `block in run'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:385:in `block in with_around_and_singleton_context_hooks'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:343:in `block in with_around_example_hooks'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:474:in `block in run'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:614:in `block in run_around_example_hooks_for'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:247:in `call'
 - rspec-rails (3.2.0) lib/rspec/rails/adapters.rb:111:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:333:in `instance_exec'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:387:in `execute_with'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:247:in `call'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:617:in `run_around_example_hooks_for'
 - rspec-core (3.2.0) lib/rspec/core/hooks.rb:474:in `run'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:343:in `with_around_example_hooks'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks'
 - rspec-core (3.2.0) lib/rspec/core/example.rb:174:in `run'
 - rspec-core (3.2.0) lib/rspec/core/example_group.rb:548:in `block in run_examples'
 - rspec-core (3.2.0) lib/rspec/core/example_group.rb:544:in `run_examples'
 - rspec-core (3.2.0) lib/rspec/core/example_group.rb:512:in `run'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:110:in `block (3 levels) in run_specs'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:110:in `block (2 levels) in run_specs'
 - rspec-core (3.2.0) lib/rspec/core/configuration.rb:1526:in `with_suite_hooks'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:109:in `block in run_specs'
 - rspec-core (3.2.0) lib/rspec/core/reporter.rb:62:in `report'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:108:in `run_specs'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:86:in `run'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:70:in `run'
 - rspec-core (3.2.0) lib/rspec/core/runner.rb:38:in `invoke'
 - rspec-core (3.2.0) exe/rspec:4:in `<top (required)>'
 - ruby/2.1.0/bin/rspec:23:in `<top (required)>'
 - -e:1:in `<main>'

导致此回溯的原因是什么?测试环境配置有问题吗?当我切换到ENV["RAILS_ENV"] ||= 'development'时,问题仍然存在,但是如果我在开发中运行应用程序,则一切正常,在auth之后我会看到正确的页面。

1 个答案:

答案 0 :(得分:0)

谢谢大家。 运行测试后,测试数据库被清除,因此我没有尝试登录的用户条目因此无法找到方法saved_filters。当我添加此用户时,我看到了我的应用程序页面。