Class: RR::DoubleDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/double_definition.rb

Overview

RR::Double is the use case for a method call. It has the ArgumentEqualityExpectation, TimesCalledExpectation, and the implementation.

Constant Summary collapse

ORIGINAL_METHOD =

:nodoc:

Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space) ⇒ DoubleDefinition

Returns a new instance of DoubleDefinition.



16
17
18
19
20
21
22
23
24
# File 'lib/rr/double_definition.rb', line 16

def initialize(space)
  @space = space
  @implementation = nil
  @argument_expectation = nil
  @times_matcher = nil
  @after_call_value = nil
  @yields_value = nil
  returns_block_callback_strategy!
end

Instance Attribute Details

#after_call_valueObject

Returns the value of attribute after_call_value.



7
8
9
# File 'lib/rr/double_definition.rb', line 7

def after_call_value
  @after_call_value
end

#argument_expectationObject

Returns the value of attribute argument_expectation.



7
8
9
# File 'lib/rr/double_definition.rb', line 7

def argument_expectation
  @argument_expectation
end

#block_callback_strategyObject (readonly)

Returns the value of attribute block_callback_strategy.



14
15
16
# File 'lib/rr/double_definition.rb', line 14

def block_callback_strategy
  @block_callback_strategy
end

#doubleObject

Returns the value of attribute double.



7
8
9
# File 'lib/rr/double_definition.rb', line 7

def double
  @double
end

#implementationObject

Returns the value of attribute implementation.



7
8
9
# File 'lib/rr/double_definition.rb', line 7

def implementation
  @implementation
end

#times_calledObject

Returns the value of attribute times_called.



7
8
9
# File 'lib/rr/double_definition.rb', line 7

def times_called
  @times_called
end

#times_matcherObject

Returns the value of attribute times_matcher.



7
8
9
# File 'lib/rr/double_definition.rb', line 7

def times_matcher
  @times_matcher
end

#yields_valueObject

Returns the value of attribute yields_value.



7
8
9
# File 'lib/rr/double_definition.rb', line 7

def yields_value
  @yields_value
end

Instance Method Details

#after_call(&block) ⇒ Object

Double#after_call creates a callback that occurs after call is called. The passed in block receives the return value of the Double being called. An Expection will be raised if no block is passed in.

mock(subject).method_name {return_value}.after_call {|return_value|}
subject.method_name # return_value

This feature is built into proxys.

mock.proxy(User).find('1') {|user| mock(user).valid? {false}}

Raises:

  • (ArgumentError)


199
200
201
202
203
# File 'lib/rr/double_definition.rb', line 199

def after_call(&block)
  raise ArgumentError, "after_call expects a block" unless block
  @after_call_value = block
  self
end

#after_call_block_callback_strategy!Object

:nodoc:



297
298
299
# File 'lib/rr/double_definition.rb', line 297

def after_call_block_callback_strategy! # :nodoc:
  @block_callback_strategy = :after_call
end

#any_number_of_times(&returns) ⇒ Object

Double#any_number_of_times sets an that the Double will be called any number of times. This effectively removes the times called expectation from the Doublen

Passing in a block sets the return value.

mock(subject).method_name.any_number_of_times


130
131
132
133
134
# File 'lib/rr/double_definition.rb', line 130

def any_number_of_times(&returns)
  @times_matcher = TimesCalledMatchers::AnyTimesMatcher.new
  install_method_callback returns
  self
end

#at_least(number, &returns) ⇒ Object

Double#at_least sets the expectation that the Double will be called at least n times. It works by creating a TimesCalledExpectation.

Passing in a block sets the return value.

mock(subject).method_name.at_least(4) {:return_value}


104
105
106
107
108
# File 'lib/rr/double_definition.rb', line 104

def at_least(number, &returns)
  @times_matcher = TimesCalledMatchers::AtLeastMatcher.new(number)
  install_method_callback returns
  self
end

#at_most(number, &returns) ⇒ Object

Double#at_most allows sets the expectation that the Double will be called at most n times. It works by creating a TimesCalledExpectation.

Passing in a block sets the return value.

mock(subject).method_name.at_most(4) {:return_value}


117
118
119
120
121
# File 'lib/rr/double_definition.rb', line 117

def at_most(number, &returns)
  @times_matcher = TimesCalledMatchers::AtMostMatcher.new(number)
  install_method_callback returns
  self
end

#exact_match?(*arguments) ⇒ Boolean

Double#exact_match? returns true when the passed in arguments exactly match the ArgumentEqualityExpectation arguments.

Returns:

  • (Boolean)


270
271
272
273
# File 'lib/rr/double_definition.rb', line 270

def exact_match?(*arguments)
  return false unless @argument_expectation
  @argument_expectation.exact_match?(*arguments)
end

#expected_argumentsObject

The Arguments that this Double expects



288
289
290
291
# File 'lib/rr/double_definition.rb', line 288

def expected_arguments
  return [] unless argument_expectation
  argument_expectation.expected_arguments
end

#implemented_by(implementation) ⇒ Object

Double#implemented_by sets the implementation of the Double. This method takes a Proc or a Method. Passing in a Method allows the Double to accept blocks.

obj = Object.new
def obj.foobar
  yield(1)
end
mock(obj).method_name.implemented_by(obj.method(:foobar))


248
249
250
251
# File 'lib/rr/double_definition.rb', line 248

def implemented_by(implementation)
  @implementation = implementation
  self
end

#implemented_by_original_methodObject

Double#implemented_by_original_method sets the implementation of the Double to be the original method. This is primarily used with proxyies.

obj = Object.new
def obj.foobar
  yield(1)
end
mock(obj).method_name.implemented_by_original_method
obj.foobar {|arg| puts arg} # puts 1


263
264
265
266
# File 'lib/rr/double_definition.rb', line 263

def implemented_by_original_method
  implemented_by ORIGINAL_METHOD
  self
end

#neverObject

Double#never sets the expectation that the Double will never be called.

This method does not accept a block because it will never be called.

mock(subject).method_name.never


68
69
70
71
# File 'lib/rr/double_definition.rb', line 68

def never
  @times_matcher = TimesCalledMatchers::IntegerMatcher.new(0)
  self
end

#once(&returns) ⇒ Object

Double#once sets the expectation that the Double will be called 1 time.

Passing in a block sets the return value.

mock(subject).method_name.once {:return_value}


79
80
81
82
83
# File 'lib/rr/double_definition.rb', line 79

def once(&returns)
  @times_matcher = TimesCalledMatchers::IntegerMatcher.new(1)
  install_method_callback returns
  self
end

#ordered(&returns) ⇒ Object

Double#ordered sets the Double to have an ordered expectation.

Passing in a block sets the return value.

mock(subject).method_name.ordered {return_value}


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rr/double_definition.rb', line 154

def ordered(&returns)
  raise(
    Errors::DoubleDefinitionError,
    "Double Definitions must have a dedicated Double to be ordered. " <<
    "For example, using instance_of does not allow ordered to be used. " <<
    "proxy the class's #new method instead."
  ) unless @double
  @ordered = true
  @space.ordered_doubles << @double unless @space.ordered_doubles.include?(@double)
  install_method_callback returns
  self
end

#ordered?Boolean

Double#ordered? returns true when the Double is ordered.

mock(subject).method_name.ordered?

Returns:

  • (Boolean)


170
171
172
# File 'lib/rr/double_definition.rb', line 170

def ordered?
  @ordered
end

#returns(value = nil, &implementation) ⇒ Object

Double#returns accepts an argument value or a block. It will raise an ArgumentError if both are passed in.

Passing in a block causes Double to return the return value of the passed in block.

Passing in an argument causes Double to return the argument.



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rr/double_definition.rb', line 227

def returns(value=nil, &implementation)
  if value && implementation
    raise ArgumentError, "returns cannot accept both an argument and a block"
  end
  if value.nil?
    implemented_by implementation
  else
    implemented_by proc {value}
  end
  self
end

#returns_block_callback_strategy!Object

:nodoc:



293
294
295
# File 'lib/rr/double_definition.rb', line 293

def returns_block_callback_strategy! # :nodoc:
  @block_callback_strategy = :returns
end

#terminal?Boolean

Returns:

  • (Boolean)


282
283
284
285
# File 'lib/rr/double_definition.rb', line 282

def terminal?
  return false unless @times_matcher
  @times_matcher.terminal?
end

#times(matcher_value, &returns) ⇒ Object

Double#times creates an TimesCalledExpectation of the passed in number.

Passing in a block sets the return value.

mock(subject).method_name.times(4) {:return_value}


142
143
144
145
146
# File 'lib/rr/double_definition.rb', line 142

def times(matcher_value, &returns)
  @times_matcher = TimesCalledMatchers::TimesCalledMatcher.create(matcher_value)
  install_method_callback returns
  self
end

#twice(&returns) ⇒ Object

Double#twice sets the expectation that the Double will be called 2 times.

Passing in a block sets the return value.

mock(subject).method_name.twice {:return_value}


91
92
93
94
95
# File 'lib/rr/double_definition.rb', line 91

def twice(&returns)
  @times_matcher = TimesCalledMatchers::IntegerMatcher.new(2)
  install_method_callback returns
  self
end

#verbose(&block) ⇒ Object

Double#verbose sets the Double to print out each method call it receives.

Passing in a block sets the return value



208
209
210
211
212
# File 'lib/rr/double_definition.rb', line 208

def verbose(&block)
  @verbose = true
  @after_call_value = block
  self
end

#verbose?Boolean

Double#verbose? returns true when verbose has been called on it. It returns true when the double is set to print each method call it receives.

Returns:

  • (Boolean)


216
217
218
# File 'lib/rr/double_definition.rb', line 216

def verbose?
  @verbose ? true : false
end

#wildcard_match?(*arguments) ⇒ Boolean

Double#wildcard_match? returns true when the passed in arguments wildcard match the ArgumentEqualityExpectation arguments.

Returns:

  • (Boolean)


277
278
279
280
# File 'lib/rr/double_definition.rb', line 277

def wildcard_match?(*arguments)
  return false unless @argument_expectation
  @argument_expectation.wildcard_match?(*arguments)
end

#with(*args, &returns) ⇒ Object

Double#with sets the expectation that the Double will receive the passed in arguments.

Passing in a block sets the return value.

mock(subject).method_name.with(1, 2) {:return_value}


32
33
34
35
36
# File 'lib/rr/double_definition.rb', line 32

def with(*args, &returns)
  @argument_expectation = Expectations::ArgumentEqualityExpectation.new(*args)
  install_method_callback returns
  self
end

#with_any_args(&returns) ⇒ Object

Double#with_any_args sets the expectation that the Double can receive any arguments.

Passing in a block sets the return value.

mock(subject).method_name.with_any_args {:return_value}


44
45
46
47
48
# File 'lib/rr/double_definition.rb', line 44

def with_any_args(&returns)
  @argument_expectation = Expectations::AnyArgumentExpectation.new
  install_method_callback returns
  self
end

#with_no_args(&returns) ⇒ Object

Double#with_no_args sets the expectation that the Double will receive no arguments.

Passing in a block sets the return value.

mock(subject).method_name.with_no_args {:return_value}


56
57
58
59
60
# File 'lib/rr/double_definition.rb', line 56

def with_no_args(&returns)
  @argument_expectation = Expectations::ArgumentEqualityExpectation.new()
  install_method_callback returns
  self
end

#yields(*args, &returns) ⇒ Object

Double#yields sets the Double to invoke a passed in block when the Double is called. An Expection will be raised if no block is passed in when the Double is called.

Passing in a block sets the return value.

mock(subject).method_name.yields(yield_arg1, yield_arg2) {return_value}
subject.method_name {|yield_arg1, yield_arg2|}


183
184
185
186
187
# File 'lib/rr/double_definition.rb', line 183

def yields(*args, &returns)
  @yields_value = args
  install_method_callback returns
  self
end