From 64f91200fe5b54386b952913ed260d34c39f96c4 Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Fri, 17 Apr 2020 17:10:31 +0200 Subject: [PATCH] Fix tests for Devise's :confirmable module 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. --- spec/features/confirmation_spec.rb | 5 ++--- spec/models/user_spec.rb | 12 ++++-------- spec/support/confirm_helpers.rb | 29 +++++++++++++++++++---------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/spec/features/confirmation_spec.rb b/spec/features/confirmation_spec.rb index a6e53872..f50df67c 100644 --- a/spec/features/confirmation_spec.rb +++ b/spec/features/confirmation_spec.rb @@ -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 @@ -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@person.com' diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a8252106..e3ab8936 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/support/confirm_helpers.rb b/spec/support/confirm_helpers.rb index 2463ea95..37dd710d 100644 --- a/spec/support/confirm_helpers.rb +++ b/spec/support/confirm_helpers.rb @@ -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