NoMethodError: undefined method `mock' for minitest and mocha

自作多情 提交于 2019-12-25 05:18:44

问题


I'm developing a gem for Rails 3 and came across an error in my test suit when running it under Ruby 1.8.7 at travis-ci.org:

/home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:874:in `_run_suite': undefined method `run' for #<TestActivist:0xb6936dd4> (NoMethodError)
69  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:866:in `map'
70  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:866:in `_run_suite'
71  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:853:in `_run_suites'
72  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:853:in `map'
73  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:853:in `_run_suites'
74  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:826:in `_run_anything'
75  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:1015:in `run_tests'
76  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:1002:in `send'
77  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:1002:in `_run'
78  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:1001:in `each'
79  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:1001:in `_run'
80  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:990:in `run'
81  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/minitest-3.5.0/lib/minitest/unit.rb:748:in `autorun'
82  from /home/travis/.rvm/gems/ruby-1.8.7-p370/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:21
83rake aborted!
84Command failed with status (1): [/home/travis/.rvm/rubies/ruby-1.8.7-p370/b...]

this is a link to the failing job: https://travis-ci.org/#!/pokonski/public_activity/jobs/2586051

Everything works just fine in Ruby > 1.8.7.

My relevant part of .gemspec:

  s.add_dependency 'activerecord', '>= 3.0.0'
  s.add_dependency 'activesupport', '>= 3.0.0'
  s.add_dependency 'actionpack', '>= 3.0.0'
  s.add_dependency 'i18n', '>= 0.5.0'

  if RUBY_VERSION == "1.8.7"
    s.add_development_dependency 'minitest', '>= 3.2.0'
  else
    s.add_development_dependency 'simplecov', '>= 0.6.4'
  end

  s.add_development_dependency 'bundler', '>= 1.1'
  s.add_development_dependency 'rake', '>= 0.9'
  s.add_development_dependency 'sqlite3'
  s.add_development_dependency 'mocha', '>= 0.12.1'
  s.add_development_dependency 'rails', '>= 3.0.0' # generators test

and Gemfile:

source :rubygems

gemspec

gem 'yard'

What's interesting, when running the tests locally with ruby-1.8.7-p370 I get an error with a different missing method:

test_resolving_values(TestCommon):
NoMethodError: undefined method `mock' for #<TestCommon:0x7ffdc8a79388>
    /home/piotrek/projekty/public_activity/test/test_common.rb:85:in `test_resolving_values'

More source code for the failing lines: https://github.com/pokonski/public_activity/blob/master/test/test_common.rb#L85

Anyone have a clue what's going on?


回答1:


Turns out it was because of the test_generators.rb which used Rails::Generators::TestCase, which in turn inherited from TestUnit.

Mocha was incorrectly monkey patching Test::Unit instead of MiniTest because of this. The solution was to require test_helper in test_generators.rb and force MiniTest gem to the 3.2.0 version.

Full solution in this commit: https://github.com/pokonski/public_activity/commit/731e953068297c7aa6f6f7e2b2251c79843943a1



来源:https://stackoverflow.com/questions/12772384/nomethoderror-undefined-method-mock-for-minitest-and-mocha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!