TypeError:类Task的超类不匹配

时间:2017-09-28 15:28:58

标签: ruby-on-rails ruby rspec rake travis-ci

我有一个Rails项目并使用RSpec进行测试。最近,我在spec_helper.rb

中添加了此内容
config.before(:suite) do
  Rails.application.load_tasks
end

如果我使用bundle exec rspec在本地运行测试,一切都很好。但是当我尝试在Travis CI上做同样的事情时,命令崩溃了:

TypeError: superclass mismatch for class Task
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:9:in `<module:Rails>'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:8:in `<module:Sprockets>'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:7:in `<top (required)>'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:274:in `require'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/railtie.rb:163:in `block in <class:Railtie>'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/railtie.rb:237:in `instance_exec'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/railtie.rb:237:in `block in run_tasks_blocks'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/railtie.rb:245:in `each'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/railtie.rb:245:in `each_registered_block'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/railtie.rb:237:in `run_tasks_blocks'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/application.rb:451:in `block in run_tasks_blocks'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/engine/railties.rb:13:in `each'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/engine/railties.rb:13:in `each'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/application.rb:451:in `run_tasks_blocks'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/engine.rb:453:in `load_tasks'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/railtie.rb:194:in `public_send'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/railtie.rb:194:in `method_missing'
    from (irb):25
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/commands/console.rb:110:in `start'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/commands/console.rb:9:in `start'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/railties-4.2.9/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

如果我调试Travis构建并手动启动bundle exec rails c并执行Rails.application.load_tasks,则会发生同样的事情。

这可能是什么问题?任何线索都将受到高度赞赏!

1 个答案:

答案 0 :(得分:2)

当您重新打开一个类以尝试重新定义它的超类时,会发生

TypeError: superclass mismatch。这是一种强制此错误的方法:

class A; end
class B < A; end
class C; end
class B < C; end # raises erorr

由于这只发生在Travis环境中,如果可能的话我会在那里进行调试:

  • 使用vim nano或类似的CLI编辑器打开/home/travis/build/my_application/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb
  • require 'pry'; binding.pry放在导致错误的行上方
  • bundle install
  • 重新运行您的测试代码
  • 进入调试器并检查来自Task的位置。由于您收到TypeError: superclass mismatch,因此必须在此行之前定义Task才重新打开它。 show-source Task应该显示定义的位置。