Skip to content

Commit

Permalink
Give a better error message with .at_least(3).times style expectations
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
  • Loading branch information
Sam Phippen committed Mar 29, 2013
1 parent 1015876 commit 996c51c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lib/rspec/mocks/error_generator.rb
Expand Up @@ -42,8 +42,8 @@ def raise_similar_message_args_error(expectation, *args_for_multiple_calls)
end

# @private
def raise_expectation_error(message, expected_received_count, actual_received_count, *args)
__raise "(#{intro}).#{message}#{format_args(*args)}\n expected: #{count_message(expected_received_count)}\n received: #{count_message(actual_received_count)}"
def raise_expectation_error(message, expected_received_count, actual_received_count, expectation_count_type, *args)
__raise "(#{intro}).#{message}#{format_args(*args)}\n expected: #{count_message(expected_received_count, expectation_count_type)}\n received: #{count_message(actual_received_count)}"
end

# @private
Expand Down Expand Up @@ -117,8 +117,9 @@ def arg_list(*args)
args.collect {|arg| arg.respond_to?(:description) ? arg.description : arg.inspect}.join(", ")
end

def count_message(count)
return "at least #{pretty_print(count.abs)}" if count < 0
def count_message(count, expectation_count_type=nil)
return "at least #{pretty_print(count.abs)}" if count < 0 || expectation_count_type == :at_least
return "at most #{pretty_print(count)}" if expectation_count_type == :at_most
return pretty_print(count)
end

Expand Down
10 changes: 8 additions & 2 deletions lib/rspec/mocks/message_expectation.rb
Expand Up @@ -168,7 +168,7 @@ def invoke(parent_stub, *args, &block)
if (@expected_received_count == 0 && !@at_least) || ((@exactly || @at_most) && (@actual_received_count == @expected_received_count))
@actual_received_count += 1
@failed_fast = true
@error_generator.raise_expectation_error(@message, @expected_received_count, @actual_received_count, *args)
@error_generator.raise_expectation_error(@message, @expected_received_count, @actual_received_count, expectation_count_type, *args)
end

@order_group.handle_order_constraint self
Expand Down Expand Up @@ -243,12 +243,18 @@ def advise(*args)
# @private
def generate_error
if similar_messages.empty?
@error_generator.raise_expectation_error(@message, @expected_received_count, @actual_received_count, *expected_args)
@error_generator.raise_expectation_error(@message, @expected_received_count, @actual_received_count, expectation_count_type, *expected_args)
else
@error_generator.raise_similar_message_args_error(self, *@similar_messages)
end
end

def expectation_count_type
return :at_least if @at_least
return :at_most if @at_most
return nil
end

def raise_out_of_order_error
@error_generator.raise_out_of_order_error @message
end
Expand Down

0 comments on commit 996c51c

Please sign in to comment.