Skip to content

Commit c7d66d9

Browse files
Merge pull request #190 from aldesantis/bugfix/confirmable-module-tests
Fix tests for Devise's :confirmable module
2 parents 03d16cf + 38af7f9 commit c7d66d9

4 files changed

Lines changed: 32 additions & 21 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddUnconfirmedEmailToSpreeUsers < ActiveRecord::Migration[5.2]
2+
def change
3+
unless column_exists?(:spree_users, :unconfirmed_email)
4+
add_column :spree_users, :unconfirmed_email, :string
5+
end
6+
end
7+
end

spec/features/confirmation_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
require 'spec_helper'
44

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

18-
scenario 'create a new user', :js do
17+
scenario 'create a new user', js: true, confirmable: false do
1918
visit spree.signup_path
2019

2120
fill_in 'Email', with: 'email@person.com'

spec/models/user_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,12 @@
8888
end
8989

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

98-
it "is not confirmable if the confirmable option is disabled" do
99-
set_confirmable_option(false)
100-
expect(Spree::User.devise_modules).to_not include(:confirmable)
95+
it "does not load Devise's :confirmable module when :confirmable is false", confirmable: false do
96+
expect(Spree::User.ancestors).not_to include(Devise::Models::Confirmable)
10197
end
10298
end
10399
end

spec/support/confirm_helpers.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
# frozen_string_literal: true
22

3-
module ConfirmHelpers
4-
def set_confirmable_option(value)
5-
if value
6-
Spree::User.devise_modules.push(:confirmable)
7-
stub_spree_preferences(Spree::Auth::Config, confirmable: true)
3+
RSpec.configure do |config|
4+
config.around do |example|
5+
if example.metadata.key?(:confirmable)
6+
old_user = Spree::User
7+
8+
begin
9+
example.run
10+
ensure
11+
Spree.const_set('User', old_user)
12+
end
813
else
9-
Spree::User.devise_modules.delete(:confirmable)
10-
stub_spree_preferences(Spree::Auth::Config, confirmable: false)
14+
example.run
1115
end
1216
end
13-
end
1417

15-
RSpec.configure do |c|
16-
c.include ConfirmHelpers
18+
config.before do |example|
19+
if example.metadata.key?(:confirmable)
20+
stub_spree_preferences(Spree::Auth::Config, confirmable: example.metadata[:confirmable])
21+
22+
Spree.send(:remove_const, :User)
23+
load File.expand_path('../../../app/models/spree/user.rb', __FILE__)
24+
end
25+
end
1726
end

0 commit comments

Comments
 (0)