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
#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 | |
# You say that rspec-mocks (and flexmock and mocha) suck because they force you to do things | |
# in a different order from the one and only order that you find to be valid, but the fact that this | |
# stub ^^ is necessary to make have_received (below) work adds the | |
# need for an extra line, binds these two lines together, and generally makes | |
# this example more difficult to understand than the one above. And this is a simple example. | |
# It's all trade-offs. | |
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) | |
# Change ^^ to mock(:collaborator_is_a_weird_looking_word).as_null_object and you don't | |
# need the next line. | |
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