Permalink
Browse files

align the examples for and_throw

- #90
  • Loading branch information...
dchelimsky committed Dec 6, 2011
1 parent c03c8d6 commit 8c6ffbaa51a0201a6375c7173f6caab2596f7fe5
Showing with 14 additions and 11 deletions.
  1. +10 −4 lib/rspec/mocks/message_expectation.rb
  2. +4 −7 spec/rspec/mocks/stub_spec.rb
@@ -22,7 +22,7 @@ def initialize(error_generator, expectation_ordering, expected_from, sym, method
@args_expectation = ArgumentExpectation.new(ArgumentMatchers::AnyArgsMatcher.new)
@consecutive = false
@exception_to_raise = nil
- @symbol_to_throw = nil
+ @args_to_throw = []
@order_group = expectation_ordering
@at_least = nil
@at_most = nil
@@ -125,13 +125,19 @@ def and_raise(exception=Exception)
@exception_to_raise = exception
end
- # Tells the object to throw a symbol when the message is received.
+ # @overload and_throw(symbol)
+ # @overload and_throw(symbol, object)
+ #
+ # Tells the object to throw a symbol (with the object if that form is
+ # used) when the message is received.
#
# @example
#
# car.stub(:go).and_throw(:out_of_gas)
+ # car.stub(:go).and_throw(:out_of_gas, :level => 0.1)
def and_throw(symbol, object = nil)
- @symbol_to_throw = [symbol, object]
+ @args_to_throw << symbol
+ @args_to_throw << object if object
end
# Tells the object to yield one or more args to a block when the message
@@ -173,7 +179,7 @@ def invoke(*args, &block)
begin
Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
- Kernel::throw *@symbol_to_throw unless @symbol_to_throw.nil?
+ Kernel::throw *@args_to_throw unless @args_to_throw.empty?
default_return_val = if !@method_block.nil?
invoke_method_block(*args, &block)
@@ -156,15 +156,12 @@ def existing_private_instance_method
it "throws when told to" do
@stub.stub(:something).and_throw(:up)
- lambda do
- @stub.something
- end.should throw_symbol(:up)
+ expect { @stub.something }.to throw_symbol(:up)
end
- it "throws with given arguments when told to" do
- @stub.stub(:something).and_throw(:up, :over => :there)
- result = catch(:up) { @stub.something }
- result.should == {:over => :there}
+ it "throws with argument when told to" do
+ @stub.stub(:something).and_throw(:up, 'high')
+ expect { @stub.something }.to throw_symbol(:up, 'high')
end
it "overrides a pre-existing method" do

1 comment on commit 8c6ffba

@pat

This comment has been minimized.

Show comment Hide comment
@pat

pat Dec 7, 2011

Contributor

That's definitely a nicer approach - thanks for cleaning it up :)

Contributor

pat commented on 8c6ffba Dec 7, 2011

That's definitely a nicer approach - thanks for cleaning it up :)

Please sign in to comment.