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

David Chelimsky dchelimsky at gmail.com
Wed Nov 5 14:59:33 EST 2008


On Wed, Nov 5, 2008 at 1:55 PM, Fernando Perez <lists at ruby-forum.com> wrote:
> 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 }
> end
>
> @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)
>    end
>
>    Item.stub!(:find_for_payment).and_return(@items)
>
>    @cart = mock_model(Order, :id => 1, :amount => 0, :tax => 0)
>    Order.stub!(:find_cart).and_return(@cart)
>
>    Item.should_receive(:find_for_payment).with(1, 1).and_return(@items)
>    get :index
>  end
> --
>
> 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?

@cart.should_receive(:amount=).with(50)

amount and amount= are two different methods :)


> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list