Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

and_raise should support intentionally raising an ArgumentError #121

Merged
merged 1 commit into from
Mar 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/rspec/mocks/message_expectation.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -179,14 +179,7 @@ def invoke(*args, &block)
@order_group.handle_order_constraint self @order_group.handle_order_constraint self


begin begin
begin raise_exception unless @exception_to_raise.nil?
raise(@exception_to_raise) unless @exception_to_raise.nil?
rescue ArgumentError => e
raise e.exception(<<-MESSAGE)
'and_raise' can only accept an Exception class if an instance can be constructed with no arguments.
#{@exception_to_raise.to_s}'s initialize method requires #{@exception_to_raise.instance_method(:initialize).arity} arguments, so you have to supply an instance instead.
MESSAGE
end
Kernel::throw(*@args_to_throw) unless @args_to_throw.empty? Kernel::throw(*@args_to_throw) unless @args_to_throw.empty?


default_return_val = if !@method_block.nil? default_return_val = if !@method_block.nil?
Expand All @@ -209,6 +202,19 @@ def invoke(*args, &block)
end end
end end


# @private
def raise_exception
if !@exception_to_raise.respond_to?(:instance_method) ||
@exception_to_raise.instance_method(:initialize).arity <= 0
raise(@exception_to_raise)
else
raise ArgumentError.new(<<-MESSAGE)
'and_raise' can only accept an Exception class if an instance can be constructed with no arguments.
#{@exception_to_raise.to_s}'s initialize method requires #{@exception_to_raise.instance_method(:initialize).arity} arguments, so you have to supply an instance instead.
MESSAGE
end
end

# @private # @private
def called_max_times? def called_max_times?
@expected_received_count != :any && @expected_received_count > 0 && @expected_received_count != :any && @expected_received_count > 0 &&
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/mocks/mock_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ def @mock.method_with_default_argument(arg={}); end
}.should raise_error(RuntimeError, "error message") }.should raise_error(RuntimeError, "error message")
end end


it "raises instance of submitted ArgumentError" do
error = ArgumentError.new("error message")
@mock.should_receive(:something).and_raise(error)
lambda {
@mock.something
}.should raise_error(ArgumentError, "error message")
end

it "fails with helpful message if submitted Exception requires constructor arguments" do it "fails with helpful message if submitted Exception requires constructor arguments" do
class ErrorWithNonZeroArgConstructor < RuntimeError class ErrorWithNonZeroArgConstructor < RuntimeError
def initialize(i_take_an_argument) def initialize(i_take_an_argument)
Expand Down