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.<br><br>I understand the Partial Mocking is controversial and dangerous if used incorrectly, but I've been finding myself using them with success.
<br>I'd advocate that Partial Mocking is a "sharp knife" that, if used correctly, can be a nice tool that simplifies tests/specs.<br>Of course when used incorrectly, it can "slice through bone".<br><br>
Anyways, here is an example. It is committed inside of the stubs branch.<br>How should we proceed?<br><blockquote>require File.dirname(__FILE__) + '/../lib/spec'<br><br>class MockableClass<br> def self.find id<br> return :original_return
<br> end<br>end<br><br>context "A partial mock" do<br><br> specify "should be able to stub objects" do<br> obj = Object.new<br> obj.should_receive(:foobar).and_return {:return_value}<br> obj.foobar.should_equal
:return_value<br> end<br><br> specify "should work at the class level" do<br> MockableClass.should_receive(:find).and_return {:stub_return}<br> MockableClass.find(1).should_equal :stub_return<br> end<br>
<br> specify "should revert to the original after each spec" do<br> MockableClass.find(1).should_equal :original_return<br> end<br><br>end<br><br></blockquote>Thanks,<br>Brian<br><br>