From 6b08b2a75a392c75c0f3b240111e2d1fdffda9dc Mon Sep 17 00:00:00 2001 From: Pedro Paiva Date: Thu, 28 Jan 2021 07:59:48 -0300 Subject: [PATCH] Fix exception raised by ActiveSupport Object#in? --- .../validate_presence_of_matcher.rb | 4 +- .../active_record_integration_spec.rb | 80 +++++++++++++++++++ spec/support/acceptance/helpers.rb | 2 + .../helpers/active_record_helpers.rb | 11 +++ .../acceptance/helpers/step_helpers.rb | 13 +++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 spec/acceptance/active_record_integration_spec.rb create mode 100644 spec/support/acceptance/helpers/active_record_helpers.rb diff --git a/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb index f3b7b5dd2..c23302091 100644 --- a/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb @@ -327,8 +327,8 @@ def association? end def collection_association? - association? && association_reflection.macro.in?( - [:has_many, :has_and_belongs_to_many], + association? && [:has_many, :has_and_belongs_to_many].include?( + association_reflection.macro, ) end diff --git a/spec/acceptance/active_record_integration_spec.rb b/spec/acceptance/active_record_integration_spec.rb new file mode 100644 index 000000000..e199ab1ea --- /dev/null +++ b/spec/acceptance/active_record_integration_spec.rb @@ -0,0 +1,80 @@ +require 'acceptance_spec_helper' + +describe 'shoulda-matchers integrates with active record' do + before do + create_active_record_project + + write_file 'Rakefile', <<-FILE + require 'active_record' + require 'sqlite3' + + namespace :db do + desc 'Create the database' + task :create do + File.unlink 'test.sqlite3' if File.exist?('test.sqlite3') + db = SQLite3::Database.new('test.sqlite3') + db.execute("CREATE TABLE users (id integer)") + db.execute("CREATE TABLE profiles (id integer, user_id integer)") + end + end + FILE + + run_rake_tasks!('db:create') + + write_file 'lib/user.rb', <<-FILE + require 'active_record' + + class User < ActiveRecord::Base + end + FILE + + write_file 'lib/profile.rb', <<-FILE + require 'active_record' + require 'user' + + class Profile < ActiveRecord::Base + belongs_to :user + validates_presence_of :user + end + FILE + + write_file 'spec/profile_spec.rb', <<-FILE + require 'spec_helper' + require 'profile' + + describe Profile do + before do + ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'test.sqlite3') + end + + it { should validate_presence_of(:user) } + end + FILE + + updating_bundle do + add_rspec_to_project + add_shoulda_matchers_to_project( + manually: true, + with_configuration: false, + ) + write_file 'spec/spec_helper.rb', <<-FILE + require 'shoulda/matchers' + + RSpec.configure do |config| + config.include(Shoulda::Matchers::ActiveModel) + end + FILE + end + end + + context 'when using both active_record and active_model libraries' do + it 'allows the use of matchers from both libraries' do + result = run_rspec_tests('spec/profile_spec.rb') + + expect(result).to have_output('1 example, 0 failures') + expect(result).to have_output( + 'is expected to validate that :user cannot be empty/falsy', + ) + end + end +end diff --git a/spec/support/acceptance/helpers.rb b/spec/support/acceptance/helpers.rb index c09a1dfa9..d3ba0ddca 100644 --- a/spec/support/acceptance/helpers.rb +++ b/spec/support/acceptance/helpers.rb @@ -1,4 +1,5 @@ require_relative 'helpers/active_model_helpers' +require_relative 'helpers/active_record_helpers' require_relative 'helpers/base_helpers' require_relative 'helpers/command_helpers' require_relative 'helpers/gem_helpers' @@ -20,6 +21,7 @@ def self.configure_example_group(example_group) end include ActiveModelHelpers + include ActiveRecordHelpers include BaseHelpers include CommandHelpers include GemHelpers diff --git a/spec/support/acceptance/helpers/active_record_helpers.rb b/spec/support/acceptance/helpers/active_record_helpers.rb new file mode 100644 index 000000000..e3cee5b5d --- /dev/null +++ b/spec/support/acceptance/helpers/active_record_helpers.rb @@ -0,0 +1,11 @@ +require_relative 'gem_helpers' + +module AcceptanceTests + module ActiveRecordHelpers + include GemHelpers + + def active_record_version + bundle_version_of('activerecord') + end + end +end diff --git a/spec/support/acceptance/helpers/step_helpers.rb b/spec/support/acceptance/helpers/step_helpers.rb index 560f64a42..ac3daf1c9 100644 --- a/spec/support/acceptance/helpers/step_helpers.rb +++ b/spec/support/acceptance/helpers/step_helpers.rb @@ -17,6 +17,19 @@ def create_active_model_project add_gem 'activemodel', active_model_version end + def create_active_record_project + create_generic_bundler_project + add_gem 'activemodel', active_model_version + add_gem 'activerecord', active_record_version + add_gem 'rake' + + if rails_version =~ '~> 6.0' + add_gem 'sqlite3', '1.4' + else + add_gem 'sqlite3', '1.3.6' + end + end + def create_generic_bundler_project fs.clean fs.create