Permalink
Browse files

Fix the way we autoload RSpec::Matchers.

`autoload`, besides being deprecated by Matz, does not work to require files
that are in unactivated gems--it only works with ruby's built in require, for
files that are available relative to a directory on the load path. Instead,
we use `const_missing` to make it work.

Closes #647.
  • Loading branch information...
1 parent 91b97be commit e6ecc0b79601cbc6e2464ffc4950b32c69299996 @myronmarston myronmarston committed Jul 16, 2012
@@ -13,6 +13,22 @@ Feature: configure expectation framework
provide a description to every example. You cannot rely on the generated
descriptions provided by rspec-expectations.
+ Scenario: rspec-expectations can be used by default if nothing is configured
+ Given a file named "example_spec.rb" with:
+ """
+ RSpec::Matchers.define :be_a_multiple_of do |factor|
+ match do |actual|
+ actual % factor == 0
+ end
+ end
+
+ describe 6 do
+ it { should be_a_multiple_of(3) }
+ end
+ """
+ When I run `rspec example_spec.rb`
+ Then the examples should all pass
+
Scenario: configure rspec-expectations (explicitly)
Given a file named "example_spec.rb" with:
"""
View
@@ -40,7 +40,6 @@ def require_rspec(path)
require_rspec 'core/version'
module RSpec
- autoload :Matchers, 'rspec/matchers'
autoload :SharedContext, 'rspec/core/shared_context'
# @private
@@ -106,6 +105,22 @@ def self.windows_os?
module Core
end
+
+ def self.const_missing(name)
+ case name
+ when :Matchers
+ # Load rspec-expectations when RSpec::Matchers is referenced. This allows
+ # people to define custom matchers (using `RSpec::Matchers.define`) before
+ # rspec-core has loaded rspec-expectations (since it delays the loading of
+ # it to allow users to configure a different assertion/expectation
+ # framework). `autoload` can't be used since it works with ruby's built-in
+ # require (e.g. for files that are available relative to a load path dir),
+ # but not with rubygems' extended require.
+ require 'rspec/expectations'
+ ::RSpec::Matchers
+ else super
+ end
+ end
end
require_rspec 'core/backward_compatibility'

0 comments on commit e6ecc0b

Please sign in to comment.