freerange / mocha

A mocking and stubbing library for Ruby

Home Page:https://mocha.jamesmead.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Upgraded from 1.0 to 2.0.

riniculous opened this issue · comments

Mocha 2.0. minitest '5.10.3'. rails 6.1.
In test helper have

require 'minitest/unit'
require 'mocha/minitest'

Any ideas?

NoMethodError: undefined method `pop' for nil:NilClass
    mocha (2.0.0) lib/mocha/mockery.rb:54:in `ensure in teardown'
    mocha (2.0.0) lib/mocha/mockery.rb:54:in `teardown'
    mocha (2.0.0) lib/mocha/hooks.rb:39:in `mocha_teardown'
    mocha (2.0.0) lib/mocha/integration/mini_test/adapter.rb:47:in `after_teardown'
    activesupport (6.1.6) lib/active_support/testing/time_helpers.rb:72:in `after_teardown'
    activesupport (6.1.6) lib/active_support/current_attributes/test_helper.rb:10:in `after_teardown'
    activerecord (6.1.6) lib/active_record/test_fixtures.rb:15:in `after_teardown'
    activesupport (6.1.6) lib/active_support/testing/setup_and_teardown.rb:51:in `after_teardown'
    minitest (5.16.3) lib/minitest/test.rb:103:in `block (4 levels) in run'
    minitest (5.16.3) lib/minitest/test.rb:195:in `capture_exceptions'
    minitest (5.16.3) lib/minitest/test.rb:102:in `block (3 levels) in run'
    minitest (5.16.3) lib/minitest/test.rb:101:in `each'
    minitest (5.16.3) lib/minitest/test.rb:101:in `block (2 levels) in run'
    minitest (5.16.3) lib/minitest.rb:296:in `time_it'
    minitest (5.16.3) lib/minitest/test.rb:94:in `block in run'
    minitest (5.16.3) lib/minitest.rb:391:in `on_signal'
    minitest (5.16.3) lib/minitest/test.rb:243:in `with_info_handler'
    minitest (5.16.3) lib/minitest/test.rb:93:in `run'
    minitest (5.16.3) lib/minitest.rb:1059:in `run_one_method'
    minitest (5.16.3) lib/minitest.rb:365:in `run_one_method'
    minitest (5.16.3) lib/minitest.rb:352:in `block (2 levels) in run'
    minitest (5.16.3) lib/minitest.rb:351:in `each'
    minitest (5.16.3) lib/minitest.rb:351:in `block in run'
    minitest (5.16.3) lib/minitest.rb:391:in `on_signal'
    minitest (5.16.3) lib/minitest.rb:378:in `with_info_handler'
    minitest (5.16.3) lib/minitest.rb:350:in `run'
    railties (6.1.6) lib/rails/test_unit/line_filtering.rb:10:in `run'
    minitest (5.16.3) lib/minitest.rb:182:in `block in __run'
    minitest (5.16.3) lib/minitest.rb:182:in `map'
    minitest (5.16.3) lib/minitest.rb:182:in `__run'
    minitest (5.16.3) lib/minitest.rb:159:in `run'
    minitest (5.16.3) lib/minitest.rb:83:in `block in autorun'

I hope you don't mind but I tidied up the formatting in the issue description.

Upgraded from 1.0 to 2.0

Can you confirm this means you've upgraded mocha from v1.0 to v2.0?

Mocha v1.0 was released in 2014, i.e. 9 years ago! Do you really mean you have upgraded from v1.0 -> v2.0? If so, can you try upgrading to the latest v1.x release first, i.e. v1.16.1. Also I'd recommend upgrading to the latest v2.x release, i.e. v2.0.2, not v2.0.0.

NoMethodError: undefined method `pop' for nil:NilClass

What command generated this ☝️ stack trace?

The stack trace suggests mocha's setup is not getting called correctly. Do you have any other test-related gems installed? Can you post your Gemfile?

What version of Ruby are you using?

Also can you try running the tests with the MOCHA_OPTIONS env var set to "debug" and post the output?

Are there any test setup methods or blocks in test_helper.rb or its associated files?

i saw a post talking about
require 'mocha/setup'
but dont see that in this code... Is that needed?

OK.. got it working... So basically fixtures being loaded beforehand had some bad data.
Caused the error test_helper to error out. Which caused the mocha/minitest to not load.. which caused the stack trace error above.
There was so much output i never saw the very first error of the fixture data.

I'm glad you got to the bottom of it! And thanks for letting me know and closing the issue. 😄

I also ran into this same error...

NoMethodError: undefined method `pop' for nil:NilClass

        @instances.pop
                  ^^^^
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/mocha-2.2.0/lib/mocha/mockery.rb:54:in `ensure in teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/mocha-2.2.0/lib/mocha/mockery.rb:54:in `teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/mocha-2.2.0/lib/mocha/hooks.rb:39:in `mocha_teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/mocha-2.2.0/lib/mocha/integration/minitest/adapter.rb:47:in `after_teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.2/lib/active_support/testing/time_helpers.rb:71:in `after_teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/actioncable-7.1.3.2/lib/action_cable/test_helper.rb:17:in `after_teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.2/lib/active_record/test_fixtures.rb:15:in `after_teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.2/lib/active_support/testing/setup_and_teardown.rb:51:in `after_teardown'
    /home/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activejob-7.1.3.2/lib/active_job/test_helper.rb:54:in `after_teardown'

And the note from @riniculous about fixtures helped get me on my way.

I had been saving cassettes in test/fixtures/vcr_cassettes but that was causing them to be loaded as regular Rails fixtures.

Moving the cassettes directory out of test/fixtures solved the problem.

@alexdunae Thanks for reporting this - I've opened #646 to see if we can provide a more useful error message in this scenario.