Class: RR::Scenario
- Inherits:
-
Object
- Object
- RR::Scenario
- 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
-
#argument_expectation ⇒ Object
readonly
Returns the value of attribute argument_expectation.
-
#times_called ⇒ Object
readonly
Returns the value of attribute times_called.
-
#times_called_expectation ⇒ Object
readonly
Returns the value of attribute times_called_expectation.
Instance Method Summary collapse
-
#call(*args, &block) ⇒ Object
Scenario#call calls the Scenario’s implementation.
-
#exact_match?(*arguments) ⇒ Boolean
Scenario#exact_match? returns true when the passed in arguments exactly match the ArgumentEqualityError arguments.
-
#implemented_by(implementation) ⇒ Object
Scenario#implemented_by sets the implementation of the Scenario.
-
#initialize(space) ⇒ Scenario
constructor
A new instance of Scenario.
-
#never ⇒ Object
Scenario#never creates an TimesCalledExpectation of 0.
-
#once(&returns) ⇒ Object
Scenario#once creates an TimesCalledExpectation of 1.
-
#ordered(&returns) ⇒ Object
Scenario#ordered sets the Scenario to have an ordered expectation.
-
#ordered? ⇒ Boolean
Scenario#ordered? returns true when the Scenario is ordered.
-
#returns(&implementation) ⇒ Object
Scenario#returns causes Scenario to return the return value of the passed in block.
-
#times(number, &returns) ⇒ Object
Scenario#twice creates an TimesCalledExpectation of the passed in number.
-
#times_called_verified? ⇒ Boolean
Scenario#times_called_verified? returns true when the TimesCalledExpectation is satisfied.
-
#twice(&returns) ⇒ Object
Scenario#twice creates an TimesCalledExpectation of 2.
-
#verify ⇒ Object
Scenario#verify verifies the the TimesCalledExpectation is satisfied for this scenario.
-
#wildcard_match?(*arguments) ⇒ Boolean
Scenario#wildcard_match? returns true when the passed in arguments wildcard match the ArgumentEqualityError arguments.
-
#with(*args, &returns) ⇒ Object
Scenario#with creates an ArgumentEqualityError for the Scenario.
-
#with_any_args(&returns) ⇒ Object
Scenario#with_any_args creates an AnyArgumentEqualityError for the Scenario.
-
#with_no_args(&returns) ⇒ Object
Scenario#with_no_args creates an ArgumentEqualityError with no arguments for the Scenario.
Constructor Details
#initialize(space) ⇒ Scenario
Returns a new instance of Scenario.
8 9 10 11 12 13 14 |
# File 'lib/rr/scenario.rb', line 8 def initialize(space) @space = space @implementation = nil @argument_expectation = nil @times_called_expectation = nil @times_called = 0 end |
Instance Attribute Details
#argument_expectation ⇒ Object (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_called ⇒ Object (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_expectation ⇒ Object (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.
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rr/scenario.rb', line 141 def call(*args, &block) @times_called_expectation.verify_input if @times_called_expectation @space.verify_ordered_scenario(self) if ordered? 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.
156 157 158 159 |
# File 'lib/rr/scenario.rb', line 156 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.
yield(1)
end
mock(obj).method_name.implemented_by(obj.method(:foobar))
131 132 133 134 |
# File 'lib/rr/scenario.rb', line 131 def implemented_by(implementation) @implementation = implementation self end |
#never ⇒ Object
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
57 58 59 60 |
# File 'lib/rr/scenario.rb', line 57 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}
67 68 69 70 71 |
# File 'lib/rr/scenario.rb', line 67 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}
102 103 104 105 106 107 |
# File 'lib/rr/scenario.rb', line 102 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?
112 113 114 |
# File 'lib/rr/scenario.rb', line 112 def ordered? @ordered end |
#returns(&implementation) ⇒ Object
Scenario#returns causes Scenario to return the return value of the passed in block.
118 119 120 |
# File 'lib/rr/scenario.rb', line 118 def returns(&implementation) implemented_by implementation 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}
90 91 92 93 94 |
# File 'lib/rr/scenario.rb', line 90 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.
170 171 172 173 |
# File 'lib/rr/scenario.rb', line 170 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}
78 79 80 81 82 |
# File 'lib/rr/scenario.rb', line 78 def twice(&returns) @times_called_expectation = Expectations::TimesCalledExpectation.new(2) returns(&returns) if returns self end |
#verify ⇒ Object
Scenario#verify verifies the the TimesCalledExpectation is satisfied for this scenario. A TimesCalledError is raised if the TimesCalledExpectation is not met.
178 179 180 181 182 |
# File 'lib/rr/scenario.rb', line 178 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.
163 164 165 166 |
# File 'lib/rr/scenario.rb', line 163 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}
22 23 24 25 26 |
# File 'lib/rr/scenario.rb', line 22 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}
34 35 36 37 38 |
# File 'lib/rr/scenario.rb', line 34 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}
46 47 48 49 50 |
# File 'lib/rr/scenario.rb', line 46 def with_no_args(&returns) @argument_expectation = Expectations::ArgumentEqualityError.new() returns(&returns) if returns self end |