[rspec-users] help with rspec'ing controller

Steve Odom steve.odom at gmail.com
Tue May 8 11:07:32 EDT 2007


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




More information about the rspec-users mailing list