Arthur
Arthur

Reputation: 145

Rspec controller test fails

i'm trying to run such test:

 it "render form to update an bundle with a specific id" do
   bundle = mock_model(Bundle)
   Bundle.stub!(:find).with("1") { bundle }

   get :edit, :locale => "en", :id => 1
   Bundle.should_receive(:find).with("1").and_return(bundle)
 end

Code from a Controller:

class BundlesController < ApplicationController
  # GET /bundles
  # GET /bundles.json
  .....

  # GET /bundles/1/edit
  def edit
    @bundle = Bundle.find(params[:id])
  end
  .....
end

But test fails with message:

BundlesController Bundle update render form to update an bundle with a specific id Failure/Error: Bundle.should_receive(:find).with("1").and_return(bundle) ().find("1") expected: 1 time received: 0 times # ./spec/controllers/bundles_controller_spec.rb:60:in `block (3 levels) in '

Can anyone helps me? Thanks!

Upvotes: 0

Views: 290

Answers (1)

jdl
jdl

Reputation: 17790

There are a couple of problems here, and maybe more as you post more of your code.

First of all, you're setting up stubs and expectations on Bundle and then showing us code that loads a Role instead.

Second, you're calling #should_receive at the end of your test. This method sets up an expectation for code that comes after it in your test. Unless you have some hidden callback that you're not showing us, this is always going to fail. Reverse the order.

Bundle.should_receive(:find).with("1").and_return(bundle)
get :edit, :locale => "en", :id => 1

Upvotes: 2

Related Questions