[rspec-devel] Partial Mocks

Brian Takita brian.takita at gmail.com
Sat Sep 30 23:07:40 EDT 2006

I added Partial Mocking, aka Class Level Mocking. For now, I added
should_receive and should_not_receive to Object, but it can be moved to
Class and/or its own module.

I understand the Partial Mocking is controversial and dangerous if used
incorrectly, but I've been finding myself using them with success.
I'd advocate that Partial Mocking is a "sharp knife" that, if used
correctly, can be a nice tool that simplifies tests/specs.
Of course when used incorrectly, it can "slice through bone".

Anyways, here is an example. It is committed inside of the stubs branch.
How should we proceed?

require File.dirname(__FILE__) + '/../lib/spec'

class MockableClass
  def self.find id
    return :original_return

context "A partial mock" do

  specify "should be able to stub objects" do
    obj = Object.new
    obj.should_receive(:foobar).and_return {:return_value}
    obj.foobar.should_equal :return_value

  specify "should work at the class level" do
    MockableClass.should_receive(:find).and_return {:stub_return}
    MockableClass.find(1).should_equal :stub_return

  specify "should revert to the original after each spec" do
    MockableClass.find(1).should_equal :original_return


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20060930/e55048bf/attachment.html 

More information about the rspec-devel mailing list