Only include ActiveRecord matchers if AR is defined

Fixes cases where you may be using ActiveModel without ActiveRecord.

Also add a test for loading without ActiveRecord.

The Cucumber feature creates a Gemfile containing ActiveModel and
shoulda-matchers, and then asserts that both can be loaded without any
errors.

The ActiveModel version used will match that of the Rails version
specified in the Appraisal Gemfile; and if no such version can be
determined, the latest will be used.

Closes #480.
This commit is contained in:
Anthony Williams 2014-04-15 14:16:32 +01:00 committed by Elliot Winkler
parent 36ed5ff7e5
commit 7f425948f8
4 changed files with 47 additions and 6 deletions

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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