Class: RR::Scenario

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space) ⇒ Scenario

Returns a new instance of Scenario.



8
9
10
11
12
13
14
15
# File 'lib/rr/scenario.rb', line 8

def initialize(space)
  @space = space
  @implementation = nil
  @argument_expectation = nil
  @times_called_expectation = nil
  @times_called = 0
  @yields = nil
end

Instance Attribute Details

#argument_expectationObject (readonly)

Returns the value of attribute argument_expectation.



6
7
8
# File 'lib/rr/scenario.rb', line 6

def argument_expectation
  @argument_expectation
end

#times_calledObject (readonly)

Returns the value of attribute times_called.



6
7
8
# File 'lib/rr/scenario.rb', line 6

def times_called
  @times_called
end

#times_called_expectationObject (readonly)

Returns the value of attribute times_called_expectation.



6
7
8
# File 'lib/rr/scenario.rb', line 6

def times_called_expectation
  @times_called_expectation
end

Instance Method Details

#call(*args, &block) ⇒ Object

Scenario#call calls the Scenario’s implementation. The return value of the implementation is returned.

A TimesCalledError is raised when the times called exceeds the expected TimesCalledExpectation.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rr/scenario.rb', line 167

def call(*args, &block)
  @times_called_expectation.verify_input if @times_called_expectation
  @space.verify_ordered_scenario(self) if ordered?
  if @yields
    unless block
      raise ArgumentError, "A Block must be passed into the method call when using yields"
    end
    block.call(*@yields)
  end
  return nil unless @implementation

  if @implementation.is_a?(Method)
    return @implementation.call(*args, &block)
  else
    args << block if block
    return @implementation.call(*args)
  end
end

#exact_match?(*arguments) ⇒ Boolean

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

Returns:

  • (Boolean)


188
189
190
191
# File 'lib/rr/scenario.rb', line 188

def exact_match?(*arguments)
  return false unless @argument_expectation 
  @argument_expectation.exact_match?(*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))


157
158
159
160
# File 'lib/rr/scenario.rb', line 157

def implemented_by(implementation)
  @implementation = implementation
  self
end

#neverObject

Scenario#never creates an TimesCalledExpectation of 0.

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

mock(subject).method_name.never


58
59
60
61
# File 'lib/rr/scenario.rb', line 58

def never
  @times_called_expectation = Expectations::TimesCalledExpectation.new(0)
  self
end

#once(&returns) ⇒ Object

Scenario#once creates an TimesCalledExpectation of 1.

Passing in a block sets the return value.

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


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

def once(&returns)
  @times_called_expectation = Expectations::TimesCalledExpectation.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}


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

def ordered(&returns)
  @ordered = true
  @space.ordered_scenarios << self unless @space.ordered_scenarios.include?(self)
  returns(&returns) if returns
  self
end

#ordered?Boolean

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

mock(subject).method_name.ordered?

Returns:

  • (Boolean)


113
114
115
# File 'lib/rr/scenario.rb', line 113

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.

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
131
# File 'lib/rr/scenario.rb', line 124

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

#times(number, &returns) ⇒ Object

Scenario#twice creates an TimesCalledExpectation of the passed in number.

Passing in a block sets the return value.

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


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

def times(number, &returns)
  @times_called_expectation = Expectations::TimesCalledExpectation.new(number)
  returns(&returns) if returns
  self
end

#times_called_verified?Boolean

Scenario#times_called_verified? returns true when the TimesCalledExpectation is satisfied.

Returns:

  • (Boolean)


202
203
204
205
# File 'lib/rr/scenario.rb', line 202

def times_called_verified?
  return false unless @times_called_expectation
  @times_called_expectation.verify
end

#twice(&returns) ⇒ Object

Scenario#twice creates an TimesCalledExpectation of 2.

Passing in a block sets the return value.

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


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

def twice(&returns)
  @times_called_expectation = Expectations::TimesCalledExpectation.new(2)
  returns(&returns) if returns
  self
end

#verifyObject

Scenario#verify verifies the the TimesCalledExpectation is satisfied for this scenario. A TimesCalledError is raised if the TimesCalledExpectation is not met.



210
211
212
213
214
# File 'lib/rr/scenario.rb', line 210

def verify
  return true unless @times_called_expectation
  @times_called_expectation.verify!
  true
end

#wildcard_match?(*arguments) ⇒ Boolean

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

Returns:

  • (Boolean)


195
196
197
198
# File 'lib/rr/scenario.rb', line 195

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

#with(*args, &returns) ⇒ Object

Scenario#with creates an ArgumentEqualityError for the Scenario. it takes a list of expected arguments.

Passing in a block sets the return value.

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


23
24
25
26
27
# File 'lib/rr/scenario.rb', line 23

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

#with_any_args(&returns) ⇒ Object

Scenario#with_any_args creates an AnyArgumentEqualityError for the Scenario.

Passing in a block sets the return value.

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


35
36
37
38
39
# File 'lib/rr/scenario.rb', line 35

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 creates an ArgumentEqualityError with no arguments for the Scenario.

Passing in a block sets the return value.

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


47
48
49
50
51
# File 'lib/rr/scenario.rb', line 47

def with_no_args(&returns)
  @argument_expectation = Expectations::ArgumentEqualityError.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|}


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

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