Skip to content

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.

Create a test helper module

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

Include the helper module

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 fabrication

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

Write your test

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