Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec stub helper method in controller spec

Found similar questions but surprisingly none, that I've found, give a simple answer...

Trying to stub a helper method in my controller spec; not quite sure what object would need to be doubled?

Controller calls this method:

#app/helpers/sessions_helper.rb

def signed_in?
  current_user.present?
end

I'd like to stub it in spec to return true/false.

like image 871
Meltemi Avatar asked Jan 18 '13 18:01

Meltemi


1 Answers

You can stub it from the controller spec:

controller.stub!(:signed_in?).and_return(true) # emulate signed in user
controller.stub!(:signed_in?).and_return(false) # emulate unsigned user

Object 'controller' is predefined in a controller specs.

UPDATE:

With RSpec 3 syntax:

allow(controller).to receive(:signed_in?).and_return(true)
allow(controller).to receive(:signed_in?).and_return(false)

Thanks to @jakeonrails for reminding.

like image 181
Andrew Kozin Avatar answered Oct 06 '22 03:10

Andrew Kozin