Skip to content

Commit

Permalink
Allow users to specify return values by passing a block to #at_least,…
Browse files Browse the repository at this point in the history
… #at_most and #exactly.

- @mock.should_receive(:to_s).at_least(:once) { "return val" }
- @mock.should_receive(:to_s).at_most(:once) { "return val" }
- @mock.should_receive(:to_s).exactly(:once) { "return val" }

Each of the above now work but previously returned nothing since the block was discarded.

Closes #40.
  • Loading branch information
myronmarston committed Feb 20, 2011
1 parent ca2d837 commit 0f09860
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/rspec/mocks/message_expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,20 @@ def with(*args, &block)
self
end

def exactly(n)
def exactly(n, &block)
@method_block = block if block
set_expected_received_count :exactly, n
self
end

def at_least(n)
def at_least(n, &block)
@method_block = block if block
set_expected_received_count :at_least, n
self
end

def at_most(n)
def at_most(n, &block)
@method_block = block if block
set_expected_received_count :at_most, n
self
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/mocks/at_least_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ module Mocks
@mock.random_call
@mock.rspec_verify
end

it "returns the value given by a block when the at least once method is called" do
@mock.should_receive(:to_s).at_least(:once) { "testing" }
@mock.to_s.should == "testing"
@mock.rspec_verify
end
end
end
end
6 changes: 6 additions & 0 deletions spec/rspec/mocks/at_most_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ module Mocks
@mock.should_receive(:random_call).at_most(:twice)
@mock.rspec_verify
end

it "returns the value given by a block when the at most once method is called" do
@mock.should_receive(:to_s).at_most(:once) { "testing" }
@mock.to_s.should == "testing"
@mock.rspec_verify
end
end
end
end
6 changes: 6 additions & 0 deletions spec/rspec/mocks/precise_counts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ module Mocks
@mock.rspec_verify
end

it "returns the value given by a block when the exactly once method is called" do
@mock.should_receive(:to_s).exactly(:once) { "testing" }
@mock.to_s.should == "testing"
@mock.rspec_verify
end

it "passes multiple calls with different args and counts" do
@mock.should_receive(:random_call).twice.with(1)
@mock.should_receive(:random_call).once.with(2)
Expand Down

0 comments on commit 0f09860

Please sign in to comment.