Permalink
Browse files

Allow users to specify return values by passing a block to #at_least,…

… #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...
1 parent ca2d837 commit 0f098604d4f60998ccbbdd052a6c38b0bb75c9c7 @myronmarston myronmarston committed Feb 20, 2011
@@ -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
@@ -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
@@ -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
@@ -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)

0 comments on commit 0f09860

Please sign in to comment.