Class: RSpec::Mocks::ErrorGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/mocks/error_generator.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ErrorGenerator) initialize(target, name)

Returns a new instance of ErrorGenerator



24
25
26
27
# File 'lib/rspec/mocks/error_generator.rb', line 24

def initialize(target, name)
  @target = target
  @name = name
end

Instance Attribute Details

- (Object) opts



30
31
32
# File 'lib/rspec/mocks/error_generator.rb', line 30

def opts
  @opts ||= {}
end

Class Method Details

+ (Object) raise_double_negation_error(wrapped_expression)



167
168
169
170
171
172
# File 'lib/rspec/mocks/error_generator.rb', line 167

def self.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

Instance Method Details

- (Object) describe_expectation(message, expected_received_count, actual_received_count, *args)



125
126
127
# File 'lib/rspec/mocks/error_generator.rb', line 125

def describe_expectation(message, expected_received_count, actual_received_count, *args)
  "have received #{message}#{format_args(*args)} #{count_message(expected_received_count)}"
end

- (Object) expected_part_of_expectation_error(expected_received_count, expectation_count_type, argument_list_matcher)



103
104
105
106
# File 'lib/rspec/mocks/error_generator.rb', line 103

def expected_part_of_expectation_error(expected_received_count, expectation_count_type, argument_list_matcher)
  "expected: #{count_message(expected_received_count, expectation_count_type)}" +
    method_call_args_description(argument_list_matcher.expected_args)
end

- (Object) method_call_args_description(args)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rspec/mocks/error_generator.rb', line 109

def method_call_args_description(args)
  case args.first
    when ArgumentMatchers::AnyArgsMatcher
      return " with any arguments"
    when ArgumentMatchers::NoArgsMatcher
      return " with no arguments"
  end

  if args.length > 0
    " with arguments: #{args.inspect.gsub(/\A\[(.+)\]\z/, '(\1)')}"
  else
    ""
  end
end

- (Object) raise_block_failed_error(message, detail)



135
136
137
# File 'lib/rspec/mocks/error_generator.rb', line 135

def raise_block_failed_error(message, detail)
  __raise "#{intro} received :#{message} but passed block failed with: #{detail}"
end

- (Object) raise_expectation_error(message, expected_received_count, argument_list_matcher, actual_received_count, expectation_count_type, *args)



61
62
63
64
65
# File 'lib/rspec/mocks/error_generator.rb', line 61

def raise_expectation_error(message, expected_received_count, argument_list_matcher, actual_received_count, expectation_count_type, *args)
  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}).#{message}#{format_args(*args)}\n    #{expected_part}\n    #{received_part}"
end

- (Object) raise_expectation_on_mocked_method(method)



162
163
164
165
# File 'lib/rspec/mocks/error_generator.rb', line 162

def raise_expectation_on_mocked_method(method)
  __raise "#{intro} expected to have received #{method}, but that " +
          "method has been mocked instead of stubbed."
end

- (Object) raise_expectation_on_unstubbed_method(method)



156
157
158
159
# File 'lib/rspec/mocks/error_generator.rb', line 156

def raise_expectation_on_unstubbed_method(method)
  __raise "#{intro} expected to have received #{method}, but that " +
          "method has not been stubbed."
end

- (Object) raise_expired_test_double_error



88
89
90
91
92
93
94
# File 'lib/rspec/mocks/error_generator.rb', line 88

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

- (Object) raise_invalid_arguments_error(verifier)



83
84
85
# File 'lib/rspec/mocks/error_generator.rb', line 83

def raise_invalid_arguments_error(verifier)
  __raise verifier.error_message
end

- (Object) raise_missing_block_error(args_to_yield)



140
141
142
# File 'lib/rspec/mocks/error_generator.rb', line 140

def raise_missing_block_error(args_to_yield)
  __raise "#{intro} asked to yield |#{arg_list(*args_to_yield)}| but no block was passed"
end

- (Object) raise_missing_default_stub_error(expectation, *args)



47
48
49
50
51
# File 'lib/rspec/mocks/error_generator.rb', line 47

def raise_missing_default_stub_error(expectation, *args)
  expected_args = format_args(*expectation.expected_args)
  actual_args = format_received_args(*args)
  __raise "#{intro} received #{expectation.message.inspect} with unexpected arguments\n  expected: #{expected_args}\n       got: #{actual_args}\n Please stub a default value first if message might be received with other args as well. \n"
end

- (Object) raise_non_public_error(method_name, visibility)

Raises:

  • (NoMethodError)


76
77
78
79
80
# File 'lib/rspec/mocks/error_generator.rb', line 76

def raise_non_public_error(method_name, visibility)
  raise NoMethodError, "%s method `%s' called on %s" % [
    visibility, method_name, intro
  ]
end

- (Object) raise_only_valid_on_a_partial_double(method)



150
151
152
153
# File 'lib/rspec/mocks/error_generator.rb', line 150

def raise_only_valid_on_a_partial_double(method)
  __raise "#{intro} is a pure test double. `#{method}` is only " +
          "available on a partial double."
end

- (Object) raise_out_of_order_error(message)



130
131
132
# File 'lib/rspec/mocks/error_generator.rb', line 130

def raise_out_of_order_error(message)
  __raise "#{intro} received :#{message} out of order"
end

- (Object) raise_similar_message_args_error(expectation, *args_for_multiple_calls)



54
55
56
57
58
# File 'lib/rspec/mocks/error_generator.rb', line 54

def raise_similar_message_args_error(expectation, *args_for_multiple_calls)
  expected_args = format_args(*expectation.expected_args)
  actual_args = args_for_multiple_calls.collect {|a| format_received_args(*a)}.join(", ")
  __raise "#{intro} received #{expectation.message.inspect} with unexpected arguments\n  expected: #{expected_args}\n       got: #{actual_args}"
end

- (Object) raise_unexpected_message_args_error(expectation, *args)



40
41
42
43
44
# File 'lib/rspec/mocks/error_generator.rb', line 40

def raise_unexpected_message_args_error(expectation, *args)
  expected_args = format_args(*expectation.expected_args)
  actual_args = format_received_args(*args)
  __raise "#{intro} received #{expectation.message.inspect} with unexpected arguments\n  expected: #{expected_args}\n       got: #{actual_args}"
end

- (Object) raise_unexpected_message_error(message, *args)



35
36
37
# File 'lib/rspec/mocks/error_generator.rb', line 35

def raise_unexpected_message_error(message, *args)
  __raise "#{intro} received unexpected message :#{message}#{arg_message(*args)}"
end

- (Object) raise_unimplemented_error(doubled_module, method_name)



68
69
70
71
72
73
# File 'lib/rspec/mocks/error_generator.rb', line 68

def raise_unimplemented_error(doubled_module, method_name)
  __raise "%s does not implement: %s" % [
    doubled_module.description,
    method_name
  ]
end

- (Object) raise_wrong_arity_error(args_to_yield, signature)



145
146
147
# File 'lib/rspec/mocks/error_generator.rb', line 145

def raise_wrong_arity_error(args_to_yield, signature)
  __raise "#{intro} yielded |#{arg_list(*args_to_yield)}| to block with #{signature.description}"
end

- (Object) received_part_of_expectation_error(actual_received_count, *args)



97
98
99
100
# File 'lib/rspec/mocks/error_generator.rb', line 97

def received_part_of_expectation_error(actual_received_count, *args)
  "received: #{count_message(actual_received_count)}" +
    method_call_args_description(args)
end