Stub Chain
In short, it allows you to do:
>> Object.stub_chain(:this, :is, :my, :chain).rerturns(true)
>> Object.this.is.my.chain
=> true
It cleans up a bunch of mocking, stubbing code. Very cool.
So in Rails I found myself with the following chain in a controller:
current_user.profile.photos.build
So naturally I tried:
User.any_instance.stub_chain(:profile, :photos, :build).returns(Photo.new)
This did not work. After some investigation I found that technically the stub_chain was working. It was just stubbing each method call on a Mocha::AnyInstance object instead of the instance of the class like I wanted. My solution:
profile = mock("Profile")
profile.stub_chain(:photos, :build).returns(Photo.new)
User.any_instance.stubs(:profile).returns(profile)
Not as concise but still much less code than without stub_chain