我使用 spork 运行测试没有问题,但是,有时我必须重新启动 spork 服务器才能让它识别新添加的路由。在这种情况下,我重新启动了 spork 服务器,现在每当我运行任何测试时都会收到以下错误消息:
Exception encountered: #<NoMethodError: undefined method `instantiate_observers' for ActiveRecord::Base:Class>
backtrace:
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/Users/mydocs/Documents/rails_projects/sample_app_rails_4/spec/spec_helper.rb:56:in `block in <top (required)>'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork.rb:69:in `call'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork.rb:69:in `block in exec_each_run'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork.rb:69:in `each'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork.rb:69:in `exec_each_run'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork/run_strategy/forking.rb:12:in `block in run'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork/forker.rb:21:in `block in initialize'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork/forker.rb:18:in `fork'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork/forker.rb:18:in `initialize'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/mydocs/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/spork-1.0.0rc3/lib/spork/server.rb:48:in `run'
/Users/mydocs/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/drb/drb.rb:1588:in `perform_without_block'
/Users/mydocs/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/drb/drb.rb:1548:in `perform'
/Users/mydocs/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/drb/drb.rb:1626:in `block (2 levels) in main_loop'
/Users/mydocs/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/drb/drb.rb:1622:in `loop'
/Users/mydocs/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/drb/drb.rb:1622:in `block in main_loop'
spork 服务器的额外重启并没有改变任何东西。关于可能导致此问题/如何解决此问题的任何想法?谢谢
编辑:从 spec_helper.rb 添加代码
Spork.each_run do
ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers <--- This is line 56
load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
end if Spork.using_spork?
添加了这个块,这样我就不再需要重置 spork 服务器来让它识别路由的更改,但是,我今天早些时候进行了这个更改,并且从那时起成功运行了许多测试。