[rspec-users] RSpec whines when I set the value of an object

Fernando Perez lists at ruby-forum.com
Wed Nov 5 14:55:39 EST 2008

In have the following code:

def index
  @items = Item.find_for_payment(session[:order_id], @site.id)
  @cart.amount = @items.sum { |item| item.price.to_i *
item.quantity.to_i }

@cart is set by a before_filter called find_cart which I stub.
find_for_payment is stubbed too.

In my spec I simply test for the following:
  it "should display cart" do
    @items = []
    1.upto(2) do |i|
      @items << mock_model(Item, :id => i, :price => 10 * i, :quantity
=> i, :order_id => 1)


    @cart = mock_model(Order, :id => 1, :amount => 0, :tax => 0)

    Item.should_receive(:find_for_payment).with(1, 1).and_return(@items)
    get :index

And I get the following error message:
Mock 'Order_1' received unexpected message :amount= with (50)

Well I know that @cart.amount will be set to 50, but why is RSpec
complaining about that?

If I put @cart.should_receive(:amount).with(50), rspec still throws an
error message. What to do?
Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list