Skip to content
This repository

Mocking not initialized in around(:each) blocks. #364

Closed
tylerhunt opened this Issue · 1 comment

2 participants

Tyler Hunt David Chelimsky
Tyler Hunt

The mocking framework appears to not be initialized properly before the example is run in an around block.

RSpec.configure do |config|
  config.mock_with :rspec
end

describe 'around mocking' do
  let!(:now) { Time.now }

  around(:each) do |example|
    Time.stub(:now).and_return(now)
    example.run
  end

  specify { Time.now.should == now }
end

This results in the following failure:

Run filtered 
F

Failures:

  1) around mocking 
     Failure/Error: Time.stub(:now).and_return(now)
     NoMethodError:
       undefined method `stub' for Time:Class
     # ./bug.rb:9

Finished in 0.00039 seconds
1 example, 1 failure

Changing the around to a before works as expected, and after example.run is called, the mocking methods are available inside the around block. It appears the source of the the bug is that RSpec::Core::Example#run_before_each is where the mocking framework is set up (@example_group_instance.setup_mocks_for_rspec), but around begins executing before this code is called.

David Chelimsky
Owner

Mocking is unsupported in around hooks. I'll leave that open until this fact gets documented.

The mocking framework is verified/reset after each example, so doing this in a before is just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.