Skip to content

Commit

Permalink
Fix tests for Devise's :confirmable module
Browse files Browse the repository at this point in the history
The `set_confirmable_options` helper was fundamentally broken: the
code being tested is only loaded once, so simply changing values in
Spree::Auth::Config does not have any effect on the modules loaded
in Spree::User.

The only way to ensure we load the right modules is removing and
reloading the Spree::User class after stubbing the configuration.
  • Loading branch information
aldesantis committed Apr 17, 2020
1 parent 03d16cf commit 64f9120
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
5 changes: 2 additions & 3 deletions spec/features/confirmation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

require 'spec_helper'

feature 'Confirmation' do
RSpec.feature 'Confirmation' do
before do
set_confirmable_option(false)
allow(Spree::UserMailer).to receive(:confirmation_instructions)
.and_return(double(deliver: true))
end
Expand All @@ -15,7 +14,7 @@
ActionMailer::Base.default_url_options[:host] = 'http://example.com'
end

scenario 'create a new user', :js do
scenario 'create a new user', js: true, confirmable: false do
visit spree.signup_path

fill_in 'Email', with: '[email protected]'
Expand Down
12 changes: 4 additions & 8 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,12 @@
end

describe "confirmable" do
it "is confirmable if the confirmable option is enabled" do
set_confirmable_option(true)
allow(Spree::UserMailer).to receive(:confirmation_instructions).and_return(double(deliver: true))
expect(Spree::User.devise_modules).to include(:confirmable)
set_confirmable_option(false)
it "loads Devise's :confirmable module when :confirmable is true", confirmable: true do
expect(Spree::User.ancestors).to include(Devise::Models::Confirmable)
end

it "is not confirmable if the confirmable option is disabled" do
set_confirmable_option(false)
expect(Spree::User.devise_modules).to_not include(:confirmable)
it "does not load Devise's :confirmable module when :confirmable is false", confirmable: false do
expect(Spree::User.ancestors).not_to include(Devise::Models::Confirmable)
end
end
end
29 changes: 19 additions & 10 deletions spec/support/confirm_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# frozen_string_literal: true

module ConfirmHelpers
def set_confirmable_option(value)
if value
Spree::User.devise_modules.push(:confirmable)
stub_spree_preferences(Spree::Auth::Config, confirmable: true)
RSpec.configure do |config|
config.around do |example|
if example.metadata.key?(:confirmable)
old_user = Spree::User

begin
example.run
ensure
Spree.const_set('User', old_user)
end
else
Spree::User.devise_modules.delete(:confirmable)
stub_spree_preferences(Spree::Auth::Config, confirmable: false)
example.run
end
end
end

RSpec.configure do |c|
c.include ConfirmHelpers
config.before do |example|
if example.metadata.key?(:confirmable)
stub_spree_preferences(Spree::Auth::Config, confirmable: example.metadata[:confirmable])

Spree.send(:remove_const, :User)
load File.expand_path('../../../app/models/spree/user.rb', __FILE__)
end
end
end

0 comments on commit 64f9120

Please sign in to comment.