Skip to content

Instantly share code, notes, and snippets.

@rentalcustard
Created November 22, 2010 15:09
Show Gist options
  • Save rentalcustard/710084 to your computer and use it in GitHub Desktop.
Save rentalcustard/710084 to your computer and use it in GitHub Desktop.
Mocks suck.
#RSpec Mocks...
it "does something with a collaborator" do
#Given
collab = mock(:collab)
it.collaborator = collab
#Then
collab.should_receive(:call).with(3)
#When
it.do_something
end
#Spies using RR...
it "does something with a collaborator" do
#Given
collab = Object.new
stub(collab).call
it.collaborator = collab
#When
it.do_something
#Then
collab.should have_received.call(3)
end
#object code:
def my_awesome_method
logger.debug("Doing stuff with #{@collaborator.inspect}")
@collaborator.do_stuff
end
#RSpec Mocks:
it "should do stuff with collaborator" do
collaborator = mock(:collaborator_is_a_weird_looking_word)
collaborator.stub!(:inspect)
collaborator.should_receive(:do_stuff)
object.collaborator = collaborator
object.my_awesome_method
end
#Spies using RR
it "should do stuff with collaborator" do
collaborator = Object.new
stub(collaborator).do_stuff
object.collaborator = collaborator
object.my_awesome_method
collaborator.should have_received.do_stuff
end
#And then... what happens to the tests when we remove the debugging code?
def my_awesome_method
@collaborator.do_stuff
end

Seriously guys, just go get RR and then do this:

Spec::Runner.configure do |config|
  config.mock_with :rr
  # or if that doesn't work due to a version incompatibility
  # config.mock_with RR::Adapters::Rspec
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment