Class: RR::DoubleDefinition
- Inherits:
-
Object
- Object
- RR::DoubleDefinition
- 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
-
#after_call_value ⇒ Object
Returns the value of attribute after_call_value.
-
#argument_expectation ⇒ Object
Returns the value of attribute argument_expectation.
-
#block_callback_strategy ⇒ Object
readonly
Returns the value of attribute block_callback_strategy.
-
#implementation ⇒ Object
Returns the value of attribute implementation.
-
#scenario ⇒ Object
Returns the value of attribute scenario.
-
#times_called ⇒ Object
Returns the value of attribute times_called.
-
#times_matcher ⇒ Object
Returns the value of attribute times_matcher.
-
#yields_value ⇒ Object
Returns the value of attribute yields_value.
Instance Method Summary collapse
-
#after_call(&block) ⇒ Object
Double#after_call creates a callback that occurs after call is called.
-
#after_call_block_callback_strategy! ⇒ Object
:nodoc:.
-
#any_number_of_times(&returns) ⇒ Object
Double#any_number_of_times sets an that the Double will be called any number of times.
-
#at_least(number, &returns) ⇒ Object
Double#at_least sets the expectation that the Double will be called at least n times.
-
#at_most(number, &returns) ⇒ Object
Double#at_most allows sets the expectation that the Double will be called at most n times.
-
#exact_match?(*arguments) ⇒ Boolean
Double#exact_match? returns true when the passed in arguments exactly match the ArgumentEqualityExpectation arguments.
-
#expected_arguments ⇒ Object
The Arguments that this Double expects.
-
#implemented_by(implementation) ⇒ Object
Double#implemented_by sets the implementation of the Double.
-
#implemented_by_original_method ⇒ Object
Double#implemented_by_original_method sets the implementation of the Double to be the original method.
-
#initialize(space) ⇒ DoubleDefinition
constructor
A new instance of DoubleDefinition.
-
#never ⇒ Object
Double#never sets the expectation that the Double will never be called.
-
#once(&returns) ⇒ Object
Double#once sets the expectation that the Double will be called 1 time.
-
#ordered(&returns) ⇒ Object
Double#ordered sets the Double to have an ordered expectation.
-
#ordered? ⇒ Boolean
Double#ordered? returns true when the Double is ordered.
-
#returns(value = nil, &implementation) ⇒ Object
Double#returns accepts an argument value or a block.
-
#returns_block_callback_strategy! ⇒ Object
:nodoc:.
- #terminal? ⇒ Boolean
-
#times(matcher_value, &returns) ⇒ Object
Double#times creates an TimesCalledExpectation of the passed in number.
-
#twice(&returns) ⇒ Object
Double#twice sets the expectation that the Double will be called 2 times.
-
#wildcard_match?(*arguments) ⇒ Boolean
Double#wildcard_match? returns true when the passed in arguments wildcard match the ArgumentEqualityExpectation arguments.
-
#with(*args, &returns) ⇒ Object
Double#with sets the expectation that the Double will receive the passed in arguments.
-
#with_any_args(&returns) ⇒ Object
Double#with_any_args sets the expectation that the Double can receive any arguments.
-
#with_no_args(&returns) ⇒ Object
Double#with_no_args sets the expectation that the Double will receive no arguments.
-
#yields(*args, &returns) ⇒ Object
Double#yields sets the Double to invoke a passed in block when the Double is called.
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_value ⇒ Object
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_expectation ⇒ Object
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_strategy ⇒ Object (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 |
#implementation ⇒ Object
Returns the value of attribute implementation.
7 8 9 |
# File 'lib/rr/double_definition.rb', line 7 def implementation @implementation end |
#scenario ⇒ Object
Returns the value of attribute scenario.
7 8 9 |
# File 'lib/rr/double_definition.rb', line 7 def scenario @scenario end |
#times_called ⇒ Object
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_matcher ⇒ Object
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_value ⇒ Object
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}}
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:
282 283 284 |
# File 'lib/rr/double_definition.rb', line 282 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.
255 256 257 258 |
# File 'lib/rr/double_definition.rb', line 255 def exact_match?(*arguments) return false unless @argument_expectation @argument_expectation.exact_match?(*arguments) end |
#expected_arguments ⇒ Object
The Arguments that this Double expects
273 274 275 276 |
# File 'lib/rr/double_definition.rb', line 273 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.
yield(1)
end
mock(obj).method_name.implemented_by(obj.method(:foobar))
233 234 235 236 |
# File 'lib/rr/double_definition.rb', line 233 def implemented_by(implementation) @implementation = implementation self end |
#implemented_by_original_method ⇒ Object
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.
yield(1)
end
mock(obj).method_name.implemented_by_original_method
obj. {|arg| puts arg} # puts 1
248 249 250 251 |
# File 'lib/rr/double_definition.rb', line 248 def implemented_by_original_method implemented_by ORIGINAL_METHOD self end |
#never ⇒ Object
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 @scenario @ordered = true @space.ordered_scenarios << @scenario unless @space.ordered_scenarios.include?(@scenario) install_method_callback returns self end |
#ordered? ⇒ Boolean
Double#ordered? returns true when the Double is ordered.
mock(subject).method_name.ordered?
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.
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rr/double_definition.rb', line 212 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:
278 279 280 |
# File 'lib/rr/double_definition.rb', line 278 def returns_block_callback_strategy! # :nodoc: @block_callback_strategy = :returns end |
#terminal? ⇒ Boolean
267 268 269 270 |
# File 'lib/rr/double_definition.rb', line 267 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 |
#wildcard_match?(*arguments) ⇒ Boolean
Double#wildcard_match? returns true when the passed in arguments wildcard match the ArgumentEqualityExpectation arguments.
262 263 264 265 |
# File 'lib/rr/double_definition.rb', line 262 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 |