Class: RR::ScenarioDefinition

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

Overview

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

Constant Summary collapse

ORIGINAL_METHOD =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space) ⇒ ScenarioDefinition

Returns a new instance of ScenarioDefinition.



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

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

Instance Attribute Details

#after_call_valueObject

Returns the value of attribute after_call_value.



7
8
9
# File 'lib/rr/scenario_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/scenario_definition.rb', line 7

def argument_expectation
  @argument_expectation
end

#doubleObject

Returns the value of attribute double.



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

def double
  @double
end

#implementationObject

Returns the value of attribute implementation.



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

def implementation
  @implementation
end

#scenarioObject

Returns the value of attribute scenario.



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

def scenario
  @scenario
end

#times_calledObject

Returns the value of attribute times_called.



7
8
9
# File 'lib/rr/scenario_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/scenario_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/scenario_definition.rb', line 7

def yields_value
  @yields_value
end

Instance Method Details

#after_call(&block) ⇒ Object

Scenario#after_call creates a callback that occurs after call is called. The passed in block receives the return value of the Scenario 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 probes.

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

Raises:

  • (ArgumentError)


198
199
200
201
202
# File 'lib/rr/scenario_definition.rb', line 198

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

#any_number_of_times(&returns) ⇒ Object

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

Passing in a block sets the return value.

mock(subject).method_name.any_number_of_times


129
130
131
132
133
# File 'lib/rr/scenario_definition.rb', line 129

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

#at_least(number, &returns) ⇒ Object

Scenario#at_least sets the expectation that the Scenario 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}


103
104
105
106
107
# File 'lib/rr/scenario_definition.rb', line 103

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

#at_most(number, &returns) ⇒ Object

Scenario#at_most allows sets the expectation that the Scenario 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}


116
117
118
119
120
# File 'lib/rr/scenario_definition.rb', line 116

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

#exact_match?(*arguments) ⇒ Boolean

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

Returns:

  • (Boolean)


254
255
256
257
# File 'lib/rr/scenario_definition.rb', line 254

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

#expected_argumentsObject

The Arguments that this Scenario expects



272
273
274
275
# File 'lib/rr/scenario_definition.rb', line 272

def expected_arguments
  return [] unless argument_expectation
  argument_expectation.expected_arguments
end

#implemented_by(implementation) ⇒ Object

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

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


232
233
234
235
# File 'lib/rr/scenario_definition.rb', line 232

def implemented_by(implementation)
  @implementation = implementation
  self
end

#implemented_by_original_methodObject

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

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


247
248
249
250
# File 'lib/rr/scenario_definition.rb', line 247

def implemented_by_original_method
  implemented_by ORIGINAL_METHOD
  self
end

#neverObject

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

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

mock(subject).method_name.never


67
68
69
70
# File 'lib/rr/scenario_definition.rb', line 67

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

#once(&returns) ⇒ Object

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

Passing in a block sets the return value.

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


78
79
80
81
82
# File 'lib/rr/scenario_definition.rb', line 78

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

#ordered(&returns) ⇒ Object

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

Passing in a block sets the return value.

mock(subject).method_name.ordered {return_value}


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

def ordered(&returns)
  raise(
    Errors::ScenarioDefinitionError,
    "Scenario Definitions must have a dedicated Scenario to be ordered. " <<
    "For example, using instance_of does not allow ordered to be used. " <<
    "probe the class's #new method instead."
  ) unless @scenario
  @ordered = true
  @space.ordered_scenarios << @scenario unless @space.ordered_scenarios.include?(@scenario)
  returns(&returns) if returns
  self
end

#ordered?Boolean

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

mock(subject).method_name.ordered?

Returns:

  • (Boolean)


169
170
171
# File 'lib/rr/scenario_definition.rb', line 169

def ordered?
  @ordered
end

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

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

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

Passing in an argument causes Scenario to return the argument.



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rr/scenario_definition.rb', line 211

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

#terminal?Boolean

Returns:

  • (Boolean)


266
267
268
269
# File 'lib/rr/scenario_definition.rb', line 266

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

#times(matcher_value, &returns) ⇒ Object

Scenario#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}


141
142
143
144
145
# File 'lib/rr/scenario_definition.rb', line 141

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

#twice(&returns) ⇒ Object

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

Passing in a block sets the return value.

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


90
91
92
93
94
# File 'lib/rr/scenario_definition.rb', line 90

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

#wildcard_match?(*arguments) ⇒ Boolean

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

Returns:

  • (Boolean)


261
262
263
264
# File 'lib/rr/scenario_definition.rb', line 261

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

#with(*args, &returns) ⇒ Object

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

Passing in a block sets the return value.

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


31
32
33
34
35
# File 'lib/rr/scenario_definition.rb', line 31

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

#with_any_args(&returns) ⇒ Object

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

Passing in a block sets the return value.

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


43
44
45
46
47
# File 'lib/rr/scenario_definition.rb', line 43

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

#with_no_args(&returns) ⇒ Object

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

Passing in a block sets the return value.

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


55
56
57
58
59
# File 'lib/rr/scenario_definition.rb', line 55

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

#yields(*args, &returns) ⇒ Object

Scenario#yields sets the Scenario to invoke a passed in block when the Scenario is called. An Expection will be raised if no block is passed in when the Scenario 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|}


182
183
184
185
186
# File 'lib/rr/scenario_definition.rb', line 182

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