[rspec-users] Mock with an attributes that has state

John D. Hume duelin.markers at gmail.com
Wed Jun 4 10:50:45 EDT 2008


On Wed, Jun 4, 2008 at 9:06 AM, Matthew Lins <mattlins at gmail.com> wrote:
> I realize I could do this differently and just do a should_receive on the
> OrderItem, looking for '+=' or something, but that doesn't feel right.

I know this isn't what you're looking for, but note that whether you do:
  item.quantity = item.quantity + other_item.quantity
or
  item.quantity += other_item.quantity
the sequence of calls to item will be the same: first item.quantity,
then item.quantity=, with + being sent to the return value of
item.quantity in between.

To try and help with what you're actually trying to do, I think you
could define a singleton method on your mocked model.
  class << @order_item1
    attr_accessor :quantity
  end
but that's smelly.

It feels to me like the logic for rolling one item into another
belongs in OrderItem. So maybe instead your order spec turns out to be
something like:
  @order_item1.should_receive(:merge).with(@order_item4)
If the logic for determining whether to merge also moved into
OrderItem, Order (and its spec) could forget about all the attributes
of OrderItem.

-hume.


More information about the rspec-users mailing list