The following code raises an error: undefined method 'any_instance' for String:Class
require 'rspec' RSpec.configure do |config| config.mock_with :rspec end describe String do it 'stubs' do String.any_instance.stub(:foo).and_return(1) ''.foo.should eq(1) end end
How can I include the Mocks module into the Class or Object class?
any_instance was recently added to rspec, so your example now works for me as it is with rspec 2.7.
The new way to do this is allow_any_instance_of(String).to receive(:foo).and_return(1)
Here is more any_instance documentation: https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/any-instance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With