Skip to content

Instantly share code, notes, and snippets.

@testobsessed
Created May 24, 2011 19:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save testobsessed/989413 to your computer and use it in GitHub Desktop.
Save testobsessed/989413 to your computer and use it in GitHub Desktop.
rspec with devise
def login_as(user)
raise "Was expecting a User. Got a #{user.class} instead. Exiting." if user.class != User
@controller.request.env['warden'] = mock(
Warden,
:authenticate => user,
:authenticate! => user,
:authenticated? => true,
:authenticate? => true
)
user.last_sign_in_at = Time.now
user.save!
end
def logout
@controller.stub(:current_user) { nil }
end
def stub_user_for_helper
# TODO: this is probably NOT the right way to do this - but couldn't find Devise::TestHelpers to do this
# would be nice to clean this up - make it just part of the context instead of needing to be
# explicitly called in the :before block.
helper.stub(:current_user) {nil}
helper.stub(:user_signed_in?) {false}
end
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
@s01ipsist
Copy link

helper.stub(:current_user).and_return(@user)
expect(helper.my_test_method).to eq "/my_test_method?lol=#{@user.id}"

worked for me. although my first attempt

expect(my_test_method).to eq "/my_test_method?lol=#{@user.id}"

did not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment