Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add #add_should_and_should_not to configuration API.

This allows users to manually add these methods to proxy objects that do not have them.

Closes #114.
  • Loading branch information...
commit a954586eff4db90e8619718c183e78b32bc405ac 1 parent 4325717
@myronmarston myronmarston authored
View
13 lib/rspec/matchers/configuration.rb
@@ -38,6 +38,19 @@ def syntax
syntaxes << :expect if Expectations::Syntax.expect_enabled?
syntaxes
end
+
+ # Adds `should` and `should_not` to the given classes
+ # or modules. This can be used to ensure `should` works
+ # properly on things like proxy objects (particular
+ # `Delegator`-subclassed objects on 1.8).
+ #
+ # @param [Array<Module>] modules the list of classes or modules
+ # to add `should` and `should_not` to.
+ def add_should_and_should_not_to(*modules)
+ modules.each do |mod|
+ Expectations::Syntax.enable_should(mod)
+ end
+ end
end
# The configuration object
View
19 spec/rspec/matchers/configuration_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'delegate'
module RSpec
module Matchers
@@ -7,6 +8,24 @@ module Matchers
RSpec::Matchers.configuration.should be_a(RSpec::Matchers::Configuration)
RSpec::Matchers.configuration.should be(RSpec::Matchers.configuration)
end
+
+ context 'on an interpreter that does not provide BasicObject', :unless => defined?(::BasicObject) do
+ before { RSpec::Expectations::Syntax.disable_should(Delegator) }
+
+ let(:klass) do
+ Class.new(SimpleDelegator) do
+ def delegated?; true; end
+ end
+ end
+
+ let(:instance) { klass.new(Object.new) }
+
+ it 'provides a means to manually add it Delegator' do
+ instance.should_not respond_to(:delegated?) # because #should is being delegated...
+ RSpec::Matchers.configuration.add_should_and_should_not_to Delegator
+ instance.should respond_to(:delegated?) # now it should work!
+ end
+ end
end
shared_examples_for "configuring the expectation syntax" do
Please sign in to comment.
Something went wrong with that request. Please try again.