Skip to content

Instantly share code, notes, and snippets.

@austenito
Created April 11, 2012 12:50
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save austenito/2359120 to your computer and use it in GitHub Desktop.
Save austenito/2359120 to your computer and use it in GitHub Desktop.
Integration Testing w/ Sorcery
describe "Shopping Cart Requests" do
let!(:user) { Fabricate(:user) }
before(:each) do
login_user_post("admin", "admin")
end
context "when I visit the shopping cart" do
it " show the logged in users' cart items " do
#Test stuff
end
end
end
RSpec.configure do |config|
config.include Sorcery::TestHelpers::Rails
end
module Sorcery
module TestHelpers
module Rails
def login_user_post(user, password)
page.driver.post(user_sessions_url, { username: user, password: password})
end
end
end
end
Fabricator(:user, :class_name => "User") do
id { sequence }
username { "admin" }
password { "admin" }
display_name { "Admin Boom"}
admin { true }
email { "whatever@whatever.com" }
salt { "asdasdastr4325234324sdfds" }
crypted_password { Sorcery::CryptoProviders::BCrypt.encrypt("secret",
"asdasdastr4325234324sdfds") }
end
@austenito
Copy link
Author

Some Notes

  • This gist is used to write integration tests using Sorcery and Capybara. Capybara doesn't allow you to login in tests without posting an actual login.
  • I added the Sorcery test helpers in the spec_helper.rb, however it be better to move it into your own test helper class.

@mattyoho
Copy link

Very nice, thanks for sharing. I might change the method name to post_user_login or simply, login_user, since the login will always be a POST request anyway, but otherwise well done.

@j3j3
Copy link

j3j3 commented May 16, 2012

What an amazing gist.

@protolif
Copy link

Thank you so much.

@full-of-foo
Copy link

👍

@kkerley
Copy link

kkerley commented Mar 13, 2014

I'm getting extremely frustrated with Capybara, Rspec, Factory_girl/Fabricator and Sorcery. I've been trying for 3 hours or so now to just log in a stupid user and nothing works. I found this gist and thought that my problems were solved, but no...they're not. I keep getting:
ActionController::RoutingError:
No route matches [POST] "/login"

Or:
NameError:
undefined local variable or method `login_path'

Depending on which I user for this:
def login_user_post(email, password)
page.driver.post(login_path, { email: email, password: password})
end

Same for new_session_path and anything else I put there. I just don't get it. Why is it so unbelievably difficult to get a user logged in with Sorcery? I built a little test site a couple weeks ago with Devise instead and it worked brilliant with no obnoxious hackery or any of this other stuff and if it wasn't going to be incredibly painful to switch over all user accounts to Devise, I would just do that.

@jeffquach
Copy link

@kkerley I'm having this exact same problem and I've wasted tons of time on this, does anyone know any hacks or ways around this?

@eirvandelden
Copy link

This gist doesn't work for me either, I also either get ActionController::RoutingError:
No route matches [POST] "/login"

Or:
NameError:
undefined local variable or method `login_path'

Or:
NameError:
undefined local variable or method `page' for #<RSpec::ExampleGroups:

@anlek
Copy link

anlek commented Sep 28, 2014

Did you guys ever figure this out?
I'm getting the undefined local variable or methodpage' for ...` issue

@tomraulet
Copy link

@anlek : same error here.

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