[rspec-users] Newbie: lambda do...end.should change(Model, :count).by(1). Doesn''t work

CarmineM
2008-05-10T12:23:54Z

Hi to everyone,

I''m an RSpec, and BDD in general, newbie so in order to learn I have
chosen to use my personal website as a tesbed.

I''m having difficulties juggling with mocks, and in particula with the
following code.

Here''s the controller action:

  def create
    @album = Album.new(params[:album])
    if @album.save
      flash[:notice] = "album saved"
    else
      flash[:notice] = "could not save album"
    end
    redirect_to albums_path
  end


And here an excerpt of the spec I''m fighting with:

  describe "create" do

    before(:each) do
      login_as(:admin)
      @album = mock_model(Album, :save => true)
      Album.stub!(:new).and_return(@album)
      @params = { :title => ''a new title'', :description
=> ''a new
description'', :category => ''a new category'' }
    end

    def do_verb(params = @params)
      post :create, :album => params
    end

    it "should change the Albums count by 1 after POST" do
      lambda do
        @album.should_receive(:save).and_return(true)
        do_verb
      end.should change(Album, :count).by(1)
    end

end

When I run the spec (in TextMate) I get the following error:

count should have been changed by 1, but was changed by 0

And the row with "lambda do" highlighted.

Where am I doing wrong?

Thanks in advance for your help.

Regards,
Carmine

P.S.:
I already have bought Peepcode''s screencast on RSpec, but
they''ve left
me only with a partial picture. Is there anything else I can use to
learn?