diff --git a/NEWS.md b/NEWS.md index 97bf79aa..37ca3bc4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ * Fix `ComparisonMatcher` so that `validate_numericality_of` comparison matchers work with large numbers (#483). +* Fix so that ActiveRecord matchers aren't included when ActiveRecord + isn't defined (i.e. if you are using ActiveModel only). + # 2.6.0 * The boolean argument to `have_db_index`'s `unique` option is now optional, for diff --git a/features/activemodel_integration.feature b/features/activemodel_integration.feature new file mode 100644 index 00000000..b410b9da --- /dev/null +++ b/features/activemodel_integration.feature @@ -0,0 +1,15 @@ +Feature: integration with ActiveModel + + Scenario: create a new project using matchers + When I generate a new ActiveModel application + And I configure the application to use "shoulda-matchers" from this project + And I write to "load_dependencies.rb" with: + """ + require 'active_model' + require 'shoulda-matchers' + + puts ActiveModel::VERSION::STRING + puts "Loaded all dependencies without errors" + """ + When I successfully run `bundle exec ruby load_dependencies.rb` + Then the output should contain "Loaded all dependencies without errors" diff --git a/features/step_definitions/activemodel_steps.rb b/features/step_definitions/activemodel_steps.rb new file mode 100644 index 00000000..f91f82a3 --- /dev/null +++ b/features/step_definitions/activemodel_steps.rb @@ -0,0 +1,21 @@ +When 'I generate a new ActiveModel application' do + steps %{ + When I run `mkdir #{APP_NAME}` + And I cd to "#{APP_NAME}" + And I run `bundle init` + } + + # Figure out the ActiveModel version to use by reusing the Rails version from + # the Appraise gemfile. + if match = File.read(ENV['BUNDLE_GEMFILE']).match(/^gem "rails", "(.*)"/) + append_to_gemfile %(gem 'activemodel', '#{ match[1] }') + else + puts "Couldn't determine which ActiveModel version to load; using latest" + append_to_gemfile %(gem 'activemodel') + end + + steps %{ + And I set the "BUNDLE_GEMFILE" environment variable to "Gemfile" + And I install gems + } +end diff --git a/lib/shoulda/matchers/integrations/test_unit.rb b/lib/shoulda/matchers/integrations/test_unit.rb index 0e918693..33af7078 100644 --- a/lib/shoulda/matchers/integrations/test_unit.rb +++ b/lib/shoulda/matchers/integrations/test_unit.rb @@ -21,12 +21,14 @@ end if defined?(ActiveSupport::TestCase) ActiveSupport::TestCase.class_eval do - include Shoulda::Matchers::ActiveRecord - extend Shoulda::Matchers::ActiveRecord - end + if defined?(Shoulda::Matchers::ActiveRecord) + include Shoulda::Matchers::ActiveRecord + extend Shoulda::Matchers::ActiveRecord + end - ActiveSupport::TestCase.class_eval do - include Shoulda::Matchers::ActiveModel - extend Shoulda::Matchers::ActiveModel + if defined?(Shoulda::Matchers::ActiveModel) + include Shoulda::Matchers::ActiveModel + extend Shoulda::Matchers::ActiveModel + end end end