I’m developing a rails application. I have an Order model that has_many
OrderItems.
I mocked the OrderItem model in my Order specs using mock_model. I
thought
I should focus my specs on each model and always mock associated models.
In my Order model I need a way to merge OrderItems which have the same
cost
and same product_id. That I can spec.
The other thing this merge helper function should do is increment the
quantity of the merged OrderItems. Below @order_item1 and @order_item4
would be merged into one item with a quantity of 2.
Here are my OrderItems mocks:
@order_item1 = mock_model(OrderItem, :valid? => true, :product_id =>
1,
:cost => 1, :null_object => true)
@order_item2 = mock_model(OrderItem, :valid? => true, :product_id =>
1,
:cost => 2, :null_object => true)
@order_item3 = mock_model(OrderItem, :valid? => true, :product_id =>
2,
:cost => 1, :null_object => true)
@order_item4 = mock_model(OrderItem, :valid? => true, :product_id =>
1,
:cost => 1, :null_object => true)
Here is the spec I wrote to check for the quantity:
it "should increment the quanity of the merged items" do
lambda {
@order.valid?
}.should change(@order_item1, :quantity).from(1).to(2)
end
How do I create an attribute for ‘quantity’ that has state on my
OrderItem
mocks?
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
don’t care how it’s incremented, I just want to make sure it’s changed.
Thanks,
Matt