[rspec-users] help with rspec'ing controller

Michael Klishin michael.s.klishin at gmail.com
Tue May 8 11:43:41 EDT 2007


Steve,

First concern is that your mock objects got stubbed after it is
returned by new method stub. I alway tend to prepair mock/stub
completely before I use it as argument or any other way.

Second is that tyou try to check that your controller's setup creates
a valid product, but you use mocks, it's up to you to decide whether
your model is valid or not, what it expects to receive and what it
returns. It looks like you get this idea from your models specs where
such a check is useful.

Anyway I feel your problem is that you stub :save! but use :save that
is unexpected.
Could you post your controller code please (create action)?

On 08/05/07, Steve Odom <steve.odom at gmail.com> wrote:
> Hi,
>
> I'm a rspec newbie. I'm having trouble with rspec'ing a simple
> controller to Create a record.
>
> I've tried a couple of approaches and still get errors:
> Mock 'remedy' expected :save! with (any args) once, but received it 0
> times
>
> -or-
>
> Mock 'Remedy' received unexpected message :save with (no args).
>
> Here are my two sample approaches:
>
> describe "Create with a valid product and authenticated user" do
>      controller_name :products
>
>      before(:each) do
>        @product= mock(Product)
>        Product.stub!(:new).and_return(@product)
>      end
>
>      it "should tell the Product model to create a new product" do
>          #mock_user_authentication(true)
>          Product.should_receive(:new).with(:params).and_return(@product)
>          post 'create', :product => :params
>      end
> end
>
> describe "Create with a valid product and authenticated user with
> everything in setup" do
>       controller_name :product
>
>      setup do
>          @product= mock(:product, :null_object => true)
>          Product.stub!(:new).and_return(@product)
>          @product.should_receive(:save!).and_return(true)
>           #mock_user_authentication(true)
>
>          post :create, :product => {'name' => 'a new product',
> 'description' => 'an editable description',
>              'user_id' => 1, 'klass' => 1}
>        end
>
>        specify "should redirect to list" do
>            response.should redirect_to(:action => 'list')
>        end
>
> Any ideas what I am doing wrong?
>
> Thanks,
>
> Steve
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


-- 
This was freedom. Losing all hope was freedom.

Chasing the beauty in programming:
www.ruby-lang.org | www.rubyonrails.org


More information about the rspec-users mailing list