在Michael Hartl的11.2教程中使用AJAX失败

时间:2014-05-19 14:54:32

标签: ruby-on-rails ajax rspec

当我运行捆绑exec rspec spec 时,我有4次失败,我真的不知道出了什么问题。我认为这是AJAX的一个问题,无法解决它。

我在控制台中的错误:

An error occurred in an after hook
  NoMethodError: undefined method `each' for nil:NilClass
  occurred at /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:878:in `teardown_fixtures'

F
An error occurred in an after hook
  NoMethodError: undefined method `each' for nil:NilClass
  occurred at /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:878:in `teardown_fixtures'

F
An error occurred in an after hook
  NoMethodError: undefined method `each' for nil:NilClass
  occurred at /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:878:in `teardown_fixtures'

F
An error occurred in an after hook
  NoMethodError: undefined method `each' for nil:NilClass
  occurred at /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/fixtures.rb:878:in `teardown_fixtures'

F......[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
..................................................................................................................DEPRECATION: `expect { }.should` is deprecated. Use `expect { }.to` instead. Called from /home/tprails/RubyOnRails/new_app/spec/requests/micropost_pages_spec.rb:41:in `block (4 levels) in <top (required)>'.
.

Failures:

  1) RelationshipsController creating a relationship with Ajax should increment the Relationship count
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `load_session' for nil:NilClass
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:151:in `load!'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:147:in `load_for_write!'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:100:in `replace'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:228:in `initialize'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:140:in `new'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:140:in `initialize'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/testing/test_request.rb:11:in `new'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/testing/test_request.rb:11:in `new'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:482:in `setup_controller_request_and_response'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-rails-2.14.1/lib/rspec/rails/adapters.rb:111:in `block (2 levels) in setup'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:237:in `instance_eval'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:237:in `instance_eval'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:21:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:85:in `block in run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:85:in `each'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:85:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:345:in `run_before_each_hooks'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:294:in `run_before_each'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:113:in `block in run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-rails-2.14.1/lib/rspec/rails/example/controller_example_group.rb:158:in `call'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-rails-2.14.1/lib/rspec/rails/example/controller_example_group.rb:158:in `block (2 levels) in <module:ControllerExampleGroup>'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:104:in `call'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:104:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:111:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `map'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:371:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `block in run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `map'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `map'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block in run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/reporter.rb:58:in `report'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:25:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
     # /home/tprails/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'

  2) RelationshipsController creating a relationship with Ajax should respond with success
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `load_session' for nil:NilClass

     [...]

  3) RelationshipsController destroying a relationship with Ajax should decrement the Relationship count
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `load_session' for nil:NilClass

     [...]

  4) RelationshipsController destroying a relationship with Ajax should respond with success
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `load_session' for nil:NilClass

     [...]

Finished in 5.91 seconds
125 examples, 4 failures

Failed examples:

rspec ./spec/controllers/relationships_controller_spec.rb:12 # RelationshipsController creating a relationship with Ajax should increment the Relationship count
rspec ./spec/controllers/relationships_controller_spec.rb:18 # RelationshipsController creating a relationship with Ajax should respond with success
rspec ./spec/controllers/relationships_controller_spec.rb:31 # RelationshipsController destroying a relationship with Ajax should decrement the Relationship count
rspec ./spec/controllers/relationships_controller_spec.rb:37 # RelationshipsController destroying a relationship with Ajax should respond with success

Randomized with seed 29007

我是否必须向Gemfile添加内容?

我希望你理解我的问题,抱歉我的英语不好

由于

1 个答案:

答案 0 :(得分:0)

NoMethodError: undefined method `each' for nil:NilClass

这意味着您的变量中有nil值 例如:

a = nil
a.each do |i|
  puts i
end

会引发同样的错误 您需要检查a

的值