[rspec-users] Stubbing Model.new w/ block?

James Hillyerd james at hillyerd.com
Fri Apr 20 17:48:11 EDT 2007

Ok, I followed the advice of the list and moved more code into my
model from my controller.  When developing tests for this new code, I
ran into a problem...

My model code creates a receipt object and sets some values on it:

@receipt = Receipt.new do |r|
  r.x = 1
  r.y = 2
  # etc

I wanted to be able to stub out Receipt.new so that I could set
expectations on the methods called on the resulting Receipt object.
If in my test setup I do:

@receipt = mock_receipt

The block part of the code will never be called.  If I do:


Then @receipt in my model will be set to the return value of the block.

I also tried something along the lines of:

Receipt.stub!(:new) do
  yield @receipt

but got some weird no block error.

Help?  Am I going about this all wrong?


James A. Hillyerd <james at hillyerd.com>
Chief Technical Officer - ActiveRain Corp

More information about the rspec-users mailing list