Class: RSpec::Mocks::ErrorGenerator Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
RSpec::Mocks::AnyInstance::ErrorGenerator
|
|
Inherits: | Object |
Defined in: | rspec-mocks/lib/rspec/mocks/error_generator.rb |
Class Method Summary
- .new(target = nil) ⇒ ErrorGenerator constructor Internal use only
Instance Attribute Summary
- #opts rw Internal use only
- #opts=(value) rw Internal use only
Instance Method Summary
- #default_error_message(expectation, expected_args, actual_args) Internal use only
- #describe_expectation(verb, message, expected_received_count, _actual_received_count, args) Internal use only
- #expectation_on_nil_message(method_name) Internal use only
- #intro(unwrapped = false) Internal use only
- #method_call_args_description(args, generic_prefix = " with arguments: ", matcher_prefix = " with ") Internal use only
- #raise_already_invoked_error(message, calling_customization) Internal use only
- #raise_cant_constrain_count_for_negated_have_received_error(count_constraint) Internal use only
- #raise_double_negation_error(wrapped_expression) Internal use only
- #raise_expectation_error(message, expected_received_count, argument_list_matcher, actual_received_count, expectation_count_type, args, backtrace_line = nil, source_id = nil) Internal use only
- #raise_expectation_on_mocked_method(method) Internal use only
- #raise_expectation_on_nil_error(method_name) Internal use only
- #raise_expectation_on_unstubbed_method(method) Internal use only
- #raise_expired_test_double_error Internal use only
- #raise_have_received_disallowed(type, reason) Internal use only
- #raise_invalid_arguments_error(verifier) Internal use only
- #raise_method_not_stubbed_error(method_name) Internal use only
- #raise_missing_block_error(args_to_yield) Internal use only
- #raise_missing_default_stub_error(expectation, args_for_multiple_calls) Internal use only
- #raise_non_public_error(method_name, visibility) Internal use only
- #raise_only_valid_on_a_partial_double(method) Internal use only
- #raise_out_of_order_error(message) Internal use only
- #raise_similar_message_args_error(expectation, args_for_multiple_calls, backtrace_line = nil) Internal use only
- #raise_unexpected_message_args_error(expectation, args_for_multiple_calls, source_id = nil) Internal use only
- #raise_unexpected_message_error(message, args) Internal use only
- #raise_unimplemented_error(doubled_module, method_name, object) Internal use only
- #raise_verifying_double_not_defined_error(ref) Internal use only
- #raise_wrong_arity_error(args_to_yield, signature) Internal use only
- #__raise(message, backtrace_line = nil, source_id = nil) private Internal use only
- #arg_list(args) private Internal use only
- #count_message(count, expectation_count_type = nil) private Internal use only
- #diff_message(expected_args, actual_args) private Internal use only
- #differ private Internal use only
- #error_message(expectation, args_for_multiple_calls) private Internal use only
- #expected_part_of_expectation_error(expected_received_count, expectation_count_type, argument_list_matcher) private Internal use only
- #format_args(args) private Internal use only
- #format_received_args(args_for_multiple_calls) private Internal use only
- #group_count(index, args) private Internal use only
- #grouped_args(args) private Internal use only
- #list_of_exactly_one_string?(args) ⇒ Boolean private Internal use only
- #notify(*args) private Internal use only
-
#prepend_to_backtrace(exception, line)
private
See additional method definition at line 336.
- #received_part_of_expectation_error(actual_received_count, args) private Internal use only
- #times(count) private Internal use only
- #unexpected_arguments_message(expected_args_string, actual_args_string) private Internal use only
- #unpack_string_args(formatted_expected_args, actual_args) private Internal use only
Instance Attribute Details
#opts (rw)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 44
def opts @opts ||= {} end
#opts=(value) (rw)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 37
attr_writer :opts
Instance Method Details
#__raise(message, backtrace_line = nil, source_id = nil) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 328
def __raise(, backtrace_line=nil, source_id=nil) = opts[: ] unless opts[: ].nil? exception = RSpec::Mocks::MockExpectationError.new( ) prepend_to_backtrace(exception, backtrace_line) if backtrace_line notify exception, :source_id => source_id end
#arg_list(args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 356
def arg_list(args) args.map { |arg| RSpec::Support::ObjectFormatter.format(arg) }.join(", ") end
#count_message(count, expectation_count_type = nil) (private)
[ GitHub ]#default_error_message(expectation, expected_args, actual_args)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 71
def (expectation, expected_args, actual_args) "#{intro} received #{expectation. .inspect} #{ (expected_args, actual_args)}".dup end
#describe_expectation(verb, message, expected_received_count, _actual_received_count, args)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 132
def describe_expectation(verb, , expected_received_count, _actual_received_count, args) "#{verb} #{}#{format_args(args)} #{ (expected_received_count)}" end
#diff_message(expected_args, actual_args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 302
def (expected_args, actual_args) formatted_expected_args = expected_args.map do |x| RSpec::Support.rspec_description_for_object(x) end formatted_expected_args, actual_args = unpack_string_args(formatted_expected_args, actual_args) differ.diff(actual_args, formatted_expected_args) end
#differ (private)
[ GitHub ]#error_message(expectation, args_for_multiple_calls) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 268
def (expectation, args_for_multiple_calls) expected_args = format_args(expectation.expected_args) actual_args = format_received_args(args_for_multiple_calls) if RSpec::Support::RubyFeatures.distincts_kw_args_from_positional_hash? expected_hash = expectation.expected_args.last actual_hash = args_for_multiple_calls.last.last if Hash === expected_hash && Hash === actual_hash && (Hash.ruby2_keywords_hash?(expected_hash) != Hash.ruby2_keywords_hash?(actual_hash)) actual_description = Hash.ruby2_keywords_hash?(actual_hash) ? " (keyword arguments)" : " (options hash)" expected_description = Hash.ruby2_keywords_hash?(expected_hash) ? " (keyword arguments)" : " (options hash)" if actual_description != expected_description actual_args += actual_description expected_args += expected_description end end end = (expectation, expected_args, actual_args) if args_for_multiple_calls.one? diff = (expectation.expected_args, args_for_multiple_calls.first) if RSpec::Mocks.configuration.color? << "\nDiff:#{diff}" unless diff.gsub(/\e\[\d+m/, '').strip.empty? else << "\nDiff:#{diff}" unless diff.strip.empty? end end end
#expectation_on_nil_message(method_name)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 215
def (method_name) "An expectation of `:#{method_name}` was set on `nil`. " \ "To allow expectations on `nil` and suppress this message, set `RSpec::Mocks.configuration.allow_message_expectations_on_nil` to `true`. " \ "To disallow expectations on `nil`, set `RSpec::Mocks.configuration.allow_message_expectations_on_nil` to `false`" end
#expected_part_of_expectation_error(expected_received_count, expectation_count_type, argument_list_matcher) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 257
def expected_part_of_expectation_error(expected_received_count, expectation_count_type, argument_list_matcher) "expected: #{ (expected_received_count, expectation_count_type)}" + method_call_args_description(argument_list_matcher.expected_args) do argument_list_matcher.expected_args.length > 0 end end
#format_args(args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 351
def format_args(args) return "(no args)" if args.empty? "(#{arg_list(args)})" end
#format_received_args(args_for_multiple_calls) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 360
def format_received_args(args_for_multiple_calls) grouped_args(args_for_multiple_calls).map do |args_for_one_call, index| "#{format_args(args_for_one_call)}#{group_count(index, args_for_multiple_calls)}" end.join("\n ") end
#group_count(index, args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 380
def group_count(index, args) " (#{times(index)})" if args.size > 1 || index > 1 end
#grouped_args(args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 376
def grouped_args(args) Hash[args.group_by { |x| x }.map { |k, v| [k, v.count] }] end
#intro(unwrapped = false)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 222
def intro(unwrapped=false) case @target when TestDouble then TestDoubleFormatter.format(@target, unwrapped) when Class then formatted = "#{@target.inspect} (class)" return formatted if unwrapped "#<#{formatted}>" when NilClass then "nil" else @target.inspect end end
#list_of_exactly_one_string?(args) ⇒ Boolean
(private)
# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 320
def list_of_exactly_one_string?(args) Array === args && args.count == 1 && String === args.first end
#method_call_args_description(args, generic_prefix = " with arguments: ", matcher_prefix = " with ")
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 235
def method_call_args_description(args, generic_prefix=" with arguments: ", matcher_prefix=" with ") case args.first when ArgumentMatchers::AnyArgsMatcher then "#{matcher_prefix}any arguments" when ArgumentMatchers::NoArgsMatcher then "#{matcher_prefix}no arguments" else if yield "#{generic_prefix}#{format_args(args)}" else "" end end end
#notify(*args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 347
def notify(*args) RSpec::Support.notify_failure(*args) end
#prepend_to_backtrace(exception, line) (private)
See additional method definition at line 336.
# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 342
def prepend_to_backtrace(exception, line) raise exception rescue RSpec::Mocks::MockExpectationError => with_backtrace with_backtrace.backtrace.unshift(line) end
#raise_already_invoked_error(message, calling_customization)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 203
def raise_already_invoked_error(, calling_customization) = "The message expectation for #{intro}.#{} has already been invoked " \ "and cannot be modified further (e.g. using `#{calling_customization}`). All message expectation " \ "customizations must be applied before it is used for the first time." notify MockExpectationAlreadyInvokedError.new( ) end
#raise_cant_constrain_count_for_negated_have_received_error(count_constraint)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 193
def raise_cant_constrain_count_for_negated_have_received_error(count_constraint) __raise "can't use #{count_constraint} when negative" end
#raise_double_negation_error(wrapped_expression)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 170
def raise_double_negation_error(wrapped_expression) __raise "Isn't life confusing enough? You've already set a " \ "negative message expectation and now you are trying to " \ "negate it again with `never`. What does an expression like " \ "`#{wrapped_expression}.not_to receive(:msg).never` even mean?" end
#raise_expectation_error(message, expected_received_count, argument_list_matcher, actual_received_count, expectation_count_type, args, backtrace_line = nil, source_id = nil)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 77
def raise_expectation_error(, expected_received_count, argument_list_matcher, actual_received_count, expectation_count_type, args, backtrace_line=nil, source_id=nil) expected_part = expected_part_of_expectation_error(expected_received_count, expectation_count_type, argument_list_matcher) received_part = received_part_of_expectation_error(actual_received_count, args) __raise "(#{intro(:unwrapped)}).#{}#{format_args(args)}\n #{expected_part}\n #{received_part}", backtrace_line, source_id end
#raise_expectation_on_mocked_method(method)
[ GitHub ]#raise_expectation_on_nil_error(method_name)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 211
def raise_expectation_on_nil_error(method_name) __raise (method_name) end
#raise_expectation_on_unstubbed_method(method)
[ GitHub ]#raise_expired_test_double_error
# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 123
def raise_expired_test_double_error raise ExpiredTestDoubleError, "#{intro} was originally created in one example but has leaked into " \ "another example and can no longer be used. rspec-mocks' doubles are " \ "designed to only last for one example, and you need to create a new " \ "one in each example you wish to use it for." end
#raise_have_received_disallowed(type, reason)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 187
def raise_have_received_disallowed(type, reason) __raise "Using #{type}(...) with the `have_received` " \ "matcher is not supported#{reason}." end
#raise_invalid_arguments_error(verifier)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 118
def raise_invalid_arguments_error(verifier) __raise verifier. end
#raise_method_not_stubbed_error(method_name)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 198
def raise_method_not_stubbed_error(method_name) __raise "The method `#{method_name}` was not stubbed or was already unstubbed" end
#raise_missing_block_error(args_to_yield)
[ GitHub ]#raise_missing_default_stub_error(expectation, args_for_multiple_calls)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 59
def raise_missing_default_stub_error(expectation, args_for_multiple_calls) __raise( (expectation, args_for_multiple_calls) + "\n Please stub a default value first if message might be received with other args as well. \n" ) end
#raise_non_public_error(method_name, visibility)
# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 111
def raise_non_public_error(method_name, visibility) raise NoMethodError, "%s method `%s' called on %s" % [ visibility, method_name, intro ] end
#raise_only_valid_on_a_partial_double(method)
[ GitHub ]#raise_out_of_order_error(message)
[ GitHub ]#raise_similar_message_args_error(expectation, args_for_multiple_calls, backtrace_line = nil)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 67
def (expectation, args_for_multiple_calls, backtrace_line=nil) __raise (expectation, args_for_multiple_calls), backtrace_line end
#raise_unexpected_message_args_error(expectation, args_for_multiple_calls, source_id = nil)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 54
def (expectation, args_for_multiple_calls, source_id=nil) __raise (expectation, args_for_multiple_calls), nil, source_id end
#raise_unexpected_message_error(message, args)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 49
def (, args) __raise "#{intro} received unexpected message :#{} with #{format_args(args)}" end
#raise_unimplemented_error(doubled_module, method_name, object)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 87
def raise_unimplemented_error(doubled_module, method_name, object) = case object when InstanceVerifyingDouble "the %s class does not implement the instance method: %s".dup << if ObjectMethodReference.for(doubled_module, method_name).implemented? ". Perhaps you meant to use `class_double` instead?" else "" end when ClassVerifyingDouble "the %s class does not implement the class method: %s".dup << if InstanceMethodReference.for(doubled_module, method_name).implemented? ". Perhaps you meant to use `instance_double` instead?" else "" end else "%s does not implement: %s" end __raise % [doubled_module.description, method_name] end
#raise_verifying_double_not_defined_error(ref)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 178
def (ref) notify(VerifyingDoubleNotDefinedError.new( "#{ref.description.inspect} is not a defined constant. " \ "Perhaps you misspelt it? " \ "Disable check with `verify_doubled_constant_names` configuration option." )) end
#raise_wrong_arity_error(args_to_yield, signature)
[ GitHub ]#received_part_of_expectation_error(actual_received_count, args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 250
def received_part_of_expectation_error(actual_received_count, args) "received: #{ (actual_received_count)}" + method_call_args_description(args) do actual_received_count > 0 && args.length > 0 end end
#times(count) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 372
def times(count) "#{count} time#{count == 1 ? '' : 's'}" end
#unexpected_arguments_message(expected_args_string, actual_args_string) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 264
def (expected_args_string, actual_args_string) "with unexpected arguments\n expected: #{expected_args_string}\n got: #{actual_args_string}" end
#unpack_string_args(formatted_expected_args, actual_args) (private)
[ GitHub ]# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 312
def unpack_string_args(formatted_expected_args, actual_args) if [formatted_expected_args, actual_args].all? { |x| list_of_exactly_one_string?(x) } [formatted_expected_args.first, actual_args.first] else [formatted_expected_args, actual_args] end end