Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tylerhunt opened this issue May 6, 2011 · 1 comment
Closed

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

tylerhunt opened this issue May 6, 2011 · 1 comment

Comments

@tylerhunt
Copy link

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.

@dchelimsky
Copy link
Contributor

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants