0

surely I'm missing something... but what!?

in my spec I've

User.should_receive(:find).with("1").and_return(@user)

and in my controller

User.find(params[:id])

green light... correct...

but if I change my controller using another method, for example first:

User.first

or even

User.all

I get green light too... I was expecting an error like

expected: 1 time
received: 0 times

could anyone give me a hint to understand that behaviour?

many thanks

EDIT

here you could find more info about this issue: https://gist.github.com/3848429#file_user_controller_spec.rb

EDIT 2

my spec now, but I still need to check with method is calling each #find...

describe "GET #show" do
  before(:each) do
    @user = mock_model(User)
    should_authorize(:show, @user)
  end

  it "assigns the requested user to @user" do
    User.should_receive(:find).with("1").twice.and_return(@user)
    get :show, id: "1"
  end
  it "renders the :show template" do
    get :show, id: "1"
    response.should render_template :show
  end
end
2
  • The behavior you're seeing is not expected so there must be something else going on. Could you post more of your spec? Oct 9, 2012 at 12:10
  • hi, @Beerlington I've edited my question adding more details. browse the Gist to see the full info. thanks!
    – enricostn
    Oct 9, 2012 at 12:22

1 Answer 1

1

I don't believe you need both User.stub!(:find)... and User.should_receive(:find)... since the should_receive basically acts like a stub. I would remove the first one and see if that that changes the behavior.

5
  • if I remove User.stub!(:find)... I get this error gist.github.com/3858583
    – enricostn
    Oct 9, 2012 at 12:45
  • Looks like it's just getting called twice. What does should_authorize(:show, @user) do? Any chance that is calling User.find? Any chance your controller is calling it twice? Oct 9, 2012 at 12:51
  • It's related to ability.rb (CanCan). You could find more on #should_authorize and my CanCan settings in the same Gist gist.github.com/3848429#file_controller_helper.rb
    – enricostn
    Oct 9, 2012 at 12:58
  • ok, using User.should_receive(:find).with("1").twice give now green light. So definitely CanCan #authorize! call User.find somewhere (I've looked at CanCan code but I couldn't find that call). Now how could I "detect" which method is calling the two #find? It would be helpful. Many thanks!!!
    – enricostn
    Oct 9, 2012 at 13:22
  • I mean something like (pseudo code) User.should_receive(:find).with("1").called_by(User.authorize!)
    – enricostn
    Oct 9, 2012 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.