-
Notifications
You must be signed in to change notification settings - Fork 229
Integration Testing with Rspec, Capybara and Fabricator
Adrian Valenzuela edited this page Jan 2, 2022
·
8 revisions
If you want to write integration tests with Sorcery, Capybara, and Fabricator you will need to login using Capybara.
Somewhere in your spec directory add the following module. spec/support/authentication.rb
is a good choice. The user_sessions_url
is the generated path to your UserSessionsController.
module Sorcery
module TestHelpers
module Rails
module Integration
def login_user_post(user, password)
page.driver.post(user_sessions_url, { username: user, password: password})
end
end
end
end
end
In spec/rails_helper.rb
, include the test helper module.
RSpec.configure do |config|
config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
config.include Sorcery::TestHelpers::Rails::Request, type: :request
config.include Sorcery::TestHelpers::Rails::Integration, type: :feature
end
Create a user_fabricator.rb file in your spec/fabricators
directory.
Fabricator(:user, :class_name => "User") do
id { sequence }
username { "admin" }
password { "admin" }
display_name { "Admin Boom"}
admin { true }
email { "[email protected]" }
salt { "asdasdastr4325234324sdfds" }
crypted_password { Sorcery::CryptoProviders::BCrypt.encrypt("secret",
"asdasdastr4325234324sdfds") }
end
You can now invoke your helper module to login a user with Capybara. current_user
will be available in your controller code when visiting pages.
describe "Integration Test" do
let!(:user) { Fabricate(:user) }
before(:each) do
login_user_post("admin", "admin")
end
context "when I visit a page" do
it "show awesome things" do
#Test stuff
end
end
end
Meta
Using Sorcery
- Activity Logging
- Brute Force Protection
- DataMapper Support
- DelayedJob Integration
- Distinguish login failure reasons
- External
- External---Microsoft-Graph-authentication
- Fetching Currently Active Users
- HTTP Basic Auth
- Integration Testing
- OAuth Landing Page
- Password-less Activation
- Remember Me
- Reset Password
- Routes Constraints
- Session Timeout
- Simple Password Authentication
- Single Table Inheritance Support
- Testing Rails
- User Activation
Contributing to Sorcery