Skip to content

Commit

Permalink
Merge pull request #426 from rspec/issue-377
Browse files Browse the repository at this point in the history
Blocks provided to `with` are used as implementation.
  • Loading branch information
xaviershay committed Oct 2, 2013
2 parents 8e1a653 + 7ef80f5 commit 253eeed
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 40 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Breaking Changes for 3.0.0:
* Make `at_least(0)` raise an error. (Sam Phippen) * Make `at_least(0)` raise an error. (Sam Phippen)
* Remove support for `require 'spec/mocks'` which had been kept * Remove support for `require 'spec/mocks'` which had been kept
in place for backwards compatibility with rspec 1 (Myron Marston). in place for backwards compatibility with rspec 1 (Myron Marston).
* Blocks provided to `with` are always used as implementation (Xavier Shay).


Enhancements: Enhancements:


Expand Down
9 changes: 7 additions & 2 deletions lib/rspec/mocks/message_expectation.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -353,8 +353,13 @@ def raise_out_of_order_error
# cart.add(Book.new(:isbn => 1934356379)) # cart.add(Book.new(:isbn => 1934356379))
# # => passes # # => passes
def with(*args, &block) def with(*args, &block)
self.inner_implementation_action = block if block_given? unless args.empty? if args.empty?
@argument_list_matcher = ArgumentListMatcher.new(*args, &block) raise ArgumentError,
"`with` must have at least one argument. Use `no_args` matcher to set the expectation of receiving no arguments."
end

self.inner_implementation_action = block
@argument_list_matcher = ArgumentListMatcher.new(*args)
self self
end end


Expand Down
7 changes: 3 additions & 4 deletions lib/rspec/mocks/verifying_message_expecation.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ def initialize(*args)
# @override # @override
def with(*args, &block) def with(*args, &block)
unless ArgumentMatchers::AnyArgsMatcher === args.first unless ArgumentMatchers::AnyArgsMatcher === args.first
expected_arity = if block expected_arity = if ArgumentMatchers::NoArgsMatcher === args.first
block.arity
elsif ArgumentMatchers::NoArgsMatcher === args.first
0 0
elsif args.length > 0 elsif args.length > 0
args.length args.length
else else
raise ArgumentError, "No arguments nor block given." # No arguments given, this will raise.
super
end end


ensure_arity!(expected_arity) ensure_arity!(expected_arity)
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/mocks/block_return_value_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
obj.stub(:foo).with('baz') { 'bar' } obj.stub(:foo).with('baz') { 'bar' }
expect(obj.foo('baz')).to eq('bar') expect(obj.foo('baz')).to eq('bar')
end end

it "returns the value of executing the block with given argument" do
obj = Object.new
obj.stub(:foo).with('baz') {|x| 'bar' + x }
expect(obj.foo('baz')).to eq('barbaz')
end
end end


%w[once twice ordered and_return].each do |method| %w[once twice ordered and_return].each do |method|
Expand Down
5 changes: 2 additions & 3 deletions spec/rspec/mocks/failing_argument_matchers_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -97,11 +97,10 @@ module Mocks
end.to raise_error(/array_including\(1,2,3\)/) end.to raise_error(/array_including\(1,2,3\)/)
end end


it "fails with block matchers" do it "fails with zero arguments" do
expect do expect do
@double.should_receive(:msg).with {|arg| expect(arg).to eq :received } @double.should_receive(:msg).with {|arg| expect(arg).to eq :received }
@double.msg :no_msg_for_you end.to raise_error(ArgumentError, /must have at least one argument/)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected: :received.*\s*.*got: :no_msg_for_you/)
end end


it "fails with sensible message when args respond to #description" do it "fails with sensible message when args respond to #description" do
Expand Down
20 changes: 0 additions & 20 deletions spec/rspec/mocks/passing_argument_matchers_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,26 +89,6 @@ module Mocks
end end
end end


context "handling block matchers" do
it "matches arguments against RSpec expectations" do
@double.should_receive(:random_call).with {|arg1, arg2, arr, *rest|
expect(arg1).to eq 5
expect(arg2.length).to be >= 3
expect(arg2.length).to be <= 10
expect(arr.map {|i| i * 2}).to eq [2,4,6]
expect(rest).to eq [:fee, "fi", 4]
}
@double.random_call 5, "hello", [1,2,3], :fee, "fi", 4
end

it "does not eval the block as the return value" do
eval_count = 0
@double.should_receive(:msg).with {|a| eval_count += 1}
@double.msg(:ignore)
expect(eval_count).to eq(1)
end
end

context "handling non-matcher arguments" do context "handling non-matcher arguments" do
it "matches non special symbol (can be removed when deprecated symbols are removed)" do it "matches non special symbol (can be removed when deprecated symbols are removed)" do
@double.should_receive(:random_call).with(:some_symbol) @double.should_receive(:random_call).with(:some_symbol)
Expand Down
12 changes: 1 addition & 11 deletions spec/rspec/mocks/verifying_message_expecation_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,21 +55,11 @@ module Mocks
end end
end end


describe 'when called with a block' do
it 'matches arity against the arity of the block' do
subject.method_finder = Proc.new { lambda {|_| } }
expect(error_generator).to receive(:raise_arity_error).
with(instance_of(ArityCalculator), 2)

subject.with {|x, y| }
end
end

describe 'when called with no arguments and no block' do describe 'when called with no arguments and no block' do
it 'raises' do it 'raises' do
expect { expect {
subject.with subject.with
}.to raise_error(ArgumentError, "No arguments nor block given.") }.to raise_error(ArgumentError)
end end
end end
end end
Expand Down

0 comments on commit 253eeed

Please sign in to comment.