I switched from Test Unit to MiniTest in jruby-1.7.13. I also use mocha/integration. My problem is that running 'rake test' brings up Mocha::ExpectationError: unexpected invocation: MyClass.new. With MyClass.new beeing a class defined in the lib folder and used in the test class. I figured out that running the tests separately workds just fine. It looks like a timing or sequence issue. I tried to circumvent it by using setup/teardown to initiate the MyClass.new before each single test, but this does not help. Do I have to mock/stub the MyClass.new?
Asked
Active
Viewed 277 times
2 Answers
0
Assuming you're using Rails 4, you need to add the following to your config/application.rb
:
config.autoload_paths += %W(#{config.root}/lib)
Kill Spring/Zeus/Spork, re-run your tests, and it should work.

Chris Kottom
- 1,249
- 10
- 11
-
I don't use Rails. I only run pure Ruby/JRuby no web application. – user2025166 Sep 29 '14 at 14:08
-
I don't understand what your line of code does. Is it some configuration which works on limits how many tests can be run? – user2025166 Sep 29 '14 at 14:15
-
To make it more clear: Before switching to MiniTest I could run all test in my project (hundrets of them in subfolders under test/lib). Now even a subfolder in the test directory will not run through. – user2025166 Sep 29 '14 at 14:18
-
The line is a Rails-specific config that's needed to auto-load classes in the `lib/` directory, so it won't help you with your issue. – Chris Kottom Sep 29 '14 at 15:43
-
Could you provide gists of your `test_helper.rb` and the test case that's failing? It's impossible to try to guess what could be the problem without seeing the code. – Chris Kottom Sep 29 '14 at 15:45
-
Here is one helper.rb – user2025166 Sep 30 '14 at 07:17
-
Sorry the code is to long, I add it by doing Answer below – user2025166 Sep 30 '14 at 07:21
0
the helper.rb sample code...
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'minitest/spec'
require 'minitest/autorun'
require 'mocha/integration'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'dibta-modbus-adapter'
require 'dibta-interface'
class MiniTest::Test
def test_order
:alpha
end
def self.runnable_methods
methods = methods_matching(/^test_/)
case self.test_order
when :random, :parallel then
max = methods.size
methods.sort.sort_by { rand max }
when :alpha, :sorted then
methods.sort ## note: Runnable is shuffeled in minitest.rb
else
raise "Unknown test_order: #{self.test_order.inspect}"
end
end
end
As you can see I started to modify test_order and runnable_methods ....
It depends on the seed "number" where the tests fail. In some cases (with special seed number) the test may even run through.

user2025166
- 21
- 3