Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a warning when the should syntax is used.

Signed-off-by: Sam Phippen <samphippen@googlemail.com>
  • Loading branch information...
commit cf4d4c9d52490103a48232502958fdd7d37c04e4 1 parent 3ac6f4e
@samphippen samphippen authored
View
8 lib/rspec/mocks/configuration.rb
@@ -81,13 +81,17 @@ def transfer_nested_constants?
def transfer_nested_constants=(val)
@transfer_nested_constants = val
end
+
+ def reset_syntaxes_to_default
+ self.syntax = [:should, :expect]
+ RSpec::Mocks::Syntax.warn_about_should!
+ end
end
def self.configuration
@configuration ||= Configuration.new
end
- configuration.syntax = [:should, :expect]
+ configuration.reset_syntaxes_to_default
end
end
-
View
24 lib/rspec/mocks/syntax.rb
@@ -4,24 +4,42 @@ module Mocks
# Provides methods for enabling and disabling the available syntaxes
# provided by rspec-mocks.
module Syntax
+ def self.warn_about_should!
+ @warn_about_should = true
+ end
+
+ def self.warn_unless_should_configured(method_name)
+ if @warn_about_should
+ RSpec.deprecate(
+ "Using #{method_name} from the old `:should` syntax without explicitly enabling the syntax.",
+ :replacement => "the new `:expect` syntax or explicitly enable `:should`"
+ )
+
+ @warn_about_should = false
+ end
+ end
# @api private
# Enables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
def self.enable_should(syntax_host = default_should_syntax_host)
+ @warn_about_should = false
return if should_enabled?(syntax_host)
syntax_host.class_exec do
def should_receive(message, opts={}, &block)
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
opts[:expected_from] ||= CallerFilter.first_non_rspec_line
::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block)
end
def should_not_receive(message, &block)
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
opts = {:expected_from => CallerFilter.first_non_rspec_line}
::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block).never
end
def stub(message_or_hash, opts={}, &block)
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
if ::Hash === message_or_hash
message_or_hash.each {|message, value| stub(message).and_return value }
else
@@ -31,29 +49,35 @@ def stub(message_or_hash, opts={}, &block)
end
def unstub(message)
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
::RSpec::Mocks.space.proxy_for(self).remove_stub(message)
end
def stub_chain(*chain, &blk)
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk)
end
def as_null_object
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
@_null_object = true
::RSpec::Mocks.proxy_for(self).as_null_object
end
def null_object?
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
defined?(@_null_object)
end
def received_message?(message, *args, &block)
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
::RSpec::Mocks.proxy_for(self).received_message?(message, *args, &block)
end
unless Class.respond_to? :any_instance
Class.class_exec do
def any_instance
+ ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
::RSpec::Mocks.any_instance_recorder_for(self)
end
end
View
79 spec/rspec/mocks/configuration_spec.rb
@@ -85,6 +85,11 @@ def sandboxed
expect(configured_syntax).to eq([:should])
end
+ it "does not warn about the should syntax" do
+ RSpec.should_not_receive(:deprecate)
+ Object.new.should_not_receive(:bees)
+ end
+
it 'is a no-op when configured a second time' do
Syntax.default_should_syntax_host.should_not_receive(:method_added)
::RSpec::Mocks::ExampleMethods.should_not_receive(:method_undefined)
@@ -110,6 +115,68 @@ def sandboxed
it 'reports that both syntaxes are enabled' do
expect(configured_syntax).to eq([:should, :expect])
end
+
+ it "does not warn about the should syntax" do
+ RSpec.should_not_receive(:deprecate)
+ Object.new.should_not_receive(:bees)
+ end
+ end
+
+ context "by default" do
+ before do
+ configure_default_syntax
+ end
+
+ let(:expected_arguments) {
+ [
+ /Using.*without explicitly enabling/,
+ {:replacement=>"the new `:expect` syntax or explicitly enable `:should`"}
+ ]
+ }
+
+ it "it warns about should once, regardless of how many times it is called" do
+ expect(RSpec).to receive(:deprecate).with(*expected_arguments)
+ o = Object.new
+ o2 = Object.new
+ o.should_receive(:bees)
+ o2.should_receive(:bees)
+
+ o.bees
+ o2.bees
+ end
+
+ it "warns about should not once, regardless of how many times it is called" do
+ expect(RSpec).to receive(:deprecate).with(*expected_arguments)
+ o = Object.new
+ o2 = Object.new
+ o.should_not_receive(:bees)
+ o2.should_not_receive(:bees)
+ end
+
+ it "warns about stubbing once, regardless of how many times it is called" do
+ expect(RSpec).to receive(:deprecate).with(*expected_arguments)
+ o = Object.new
+ o2 = Object.new
+
+ o.stub(:faces)
+ o2.stub(:faces)
+ end
+
+ it "doesn't warn about stubbing after a reset and setting should" do
+ expect(RSpec).not_to receive(:deprecate)
+ RSpec::Mocks.configuration.reset_syntaxes_to_default
+ RSpec::Mocks.configuration.syntax = :should
+ o = Object.new
+ o2 = Object.new
+ o.stub(:faces)
+ o2.stub(:faces)
+ end
+
+ it "includes the call site in the deprecation warning" do
+ obj = Object.new
+ expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
+ obj.stub(:faces)
+ end
end
end
@@ -122,6 +189,10 @@ def configure_syntax(syntax)
def configured_syntax
RSpec::Mocks.configuration.syntax
end
+
+ def configure_default_syntax
+ RSpec::Mocks.configuration.reset_syntaxes_to_default
+ end
end
end
@@ -142,6 +213,14 @@ def configured_syntax
end
end
end
+
+ def configure_default_syntax
+ RSpec.configure do |rspec|
+ rspec.mock_with :rspec do |c|
+ c.reset_syntaxes_to_default
+ end
+ end
+ end
end
end
end
View
6 spec/spec_helper.rb
@@ -79,3 +79,9 @@ def reset(object)
RSpec::Mocks.instance_variable_set(:@configuration, orig_configuration)
end
end
+
+def expect_deprecation_with_call_site(file, line)
+ expect(RSpec.configuration.reporter).to receive(:deprecation) do |options|
+ expect(options[:call_site]).to include([file, line].join(':'))
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.