Skip to content

Commit

Permalink
align the examples for and_throw
Browse files Browse the repository at this point in the history
- #90
  • Loading branch information
dchelimsky committed Dec 6, 2011
1 parent c03c8d6 commit 8c6ffba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
14 changes: 10 additions & 4 deletions lib/rspec/mocks/message_expectation.rb
Expand Up @@ -22,7 +22,7 @@ def initialize(error_generator, expectation_ordering, expected_from, sym, method
@args_expectation = ArgumentExpectation.new(ArgumentMatchers::AnyArgsMatcher.new) @args_expectation = ArgumentExpectation.new(ArgumentMatchers::AnyArgsMatcher.new)
@consecutive = false @consecutive = false
@exception_to_raise = nil @exception_to_raise = nil
@symbol_to_throw = nil @args_to_throw = []
@order_group = expectation_ordering @order_group = expectation_ordering
@at_least = nil @at_least = nil
@at_most = nil @at_most = nil
Expand Down Expand Up @@ -125,13 +125,19 @@ def and_raise(exception=Exception)
@exception_to_raise = exception @exception_to_raise = exception
end 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 # @example
# #
# car.stub(:go).and_throw(:out_of_gas) # 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) def and_throw(symbol, object = nil)
@symbol_to_throw = [symbol, object] @args_to_throw << symbol
@args_to_throw << object if object
end end


# Tells the object to yield one or more args to a block when the message # Tells the object to yield one or more args to a block when the message
Expand Down Expand Up @@ -173,7 +179,7 @@ def invoke(*args, &block)


begin begin
Kernel::raise @exception_to_raise unless @exception_to_raise.nil? 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? default_return_val = if !@method_block.nil?
invoke_method_block(*args, &block) invoke_method_block(*args, &block)
Expand Down
11 changes: 4 additions & 7 deletions spec/rspec/mocks/stub_spec.rb
Expand Up @@ -156,15 +156,12 @@ def existing_private_instance_method


it "throws when told to" do it "throws when told to" do
@stub.stub(:something).and_throw(:up) @stub.stub(:something).and_throw(:up)
lambda do expect { @stub.something }.to throw_symbol(:up)
@stub.something
end.should throw_symbol(:up)
end end


it "throws with given arguments when told to" do it "throws with argument when told to" do
@stub.stub(:something).and_throw(:up, :over => :there) @stub.stub(:something).and_throw(:up, 'high')
result = catch(:up) { @stub.something } expect { @stub.something }.to throw_symbol(:up, 'high')
result.should == {:over => :there}
end end


it "overrides a pre-existing method" do it "overrides a pre-existing method" do
Expand Down

1 comment on commit 8c6ffba

@pat
Copy link
Contributor

@pat pat commented on 8c6ffba Dec 7, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.