[rspec-users] help with rspec'ing controller

aslak hellesoy aslak.hellesoy at gmail.com
Tue May 8 11:42:05 EDT 2007


Looks like you've configured your mock to receive a call to #save!
while your controller is actually calling #save. #save! is not the
same as #save!

On 5/8/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
>


More information about the rspec-users mailing list