Skip to content

Commit

Permalink
Add a warning when the should syntax is used.
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
  • Loading branch information
Sam Phippen committed Sep 8, 2013
1 parent 3ac6f4e commit 75f9f60
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rspec/mocks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

26 changes: 26 additions & 0 deletions lib/rspec/mocks/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,44 @@ 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

self.warn_about_should!

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
Expand All @@ -31,29 +51,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
Expand Down
77 changes: 77 additions & 0 deletions spec/rspec/mocks/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -110,6 +115,66 @@ 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

after 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
end
end

Expand All @@ -122,6 +187,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

Expand All @@ -142,6 +211,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
Expand Down

0 comments on commit 75f9f60

Please sign in to comment.