gpt4 book ai didi

ruby-on-rails - Undefined method 'instantiate_observers' 重启spork server后报错

转载 作者:行者123 更新时间:2023-12-04 04:46:31 24 4
gpt4 key购买 nike

我使用 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 服务器来让它识别路由的更改,但是,我今天早些时候做了这个更改,从那时起已经成功运行了许多测试。

最佳答案

观察者已从 Rails 4 中删除。尝试实例化它们是行不通的。您拥有的代码示例可能来自 Rails 3。

删除该行。不会有什么不好的事情发生。

关于ruby-on-rails - Undefined method 'instantiate_observers' 重启spork server后报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18095864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com