Class: Sus::Receive
- Inherits:
-
Object
- Object
- Sus::Receive
- Defined in:
- lib/sus/receive.rb
Overview
Represents an expectation that a method will be called on an object.
Defined Under Namespace
Classes: Times, WithArguments, WithBlock, WithOptions
Instance Method Summary collapse
-
#and_raise ⇒ Object
Specify that the method should raise an exception when called.
-
#and_return(*returning, &block) ⇒ Object
Specify the value to return when the method is called.
-
#call(assertions, subject) ⇒ Object
Evaluate this expectation against a subject.
- #call_original? ⇒ Boolean
-
#initialize(base, method, &block) ⇒ Receive
constructor
Initialize a new Receive expectation.
-
#once ⇒ Object
Specify that the method should be called exactly once.
-
#print(output) ⇒ Object
Print a representation of this expectation.
-
#twice ⇒ Object
Specify that the method should be called exactly twice.
-
#validate(mock, assertions, arguments, options, block) ⇒ Object
Validate the method call arguments, options, and block.
-
#with(*arguments, **options) ⇒ Object
Specify that the method should be called with specific arguments and options.
-
#with_arguments(predicate) ⇒ Object
Specify that the method should be called with specific arguments.
-
#with_block(predicate = Be.new(:!=, nil)) ⇒ Object
Specify that the method should be called with a block.
-
#with_call_count(predicate) ⇒ Object
Specify a predicate to match against the call count.
-
#with_options(predicate) ⇒ Object
Specify that the method should be called with specific keyword options.
Constructor Details
#initialize(base, method, &block) ⇒ Receive
Initialize a new Receive expectation.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sus/receive.rb', line 15 def initialize(base, method, &block) @base = base @method = method @times = Times.new @arguments = nil @options = nil @block = nil @returning = block end |
Instance Method Details
#and_raise ⇒ Object
Specify that the method should raise an exception when called.
113 114 115 116 117 118 119 |
# File 'lib/sus/receive.rb', line 113 def and_raise(...) @returning = proc do raise(...) end return self end |
#and_return(*returning, &block) ⇒ Object
Specify the value to return when the method is called.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sus/receive.rb', line 94 def and_return(*returning, &block) if block_given? if returning.any? raise ArgumentError, "Cannot specify both a block and returning values." end @returning = block elsif returning.size == 1 @returning = proc{returning.first} else @returning = proc{returning} end return self end |
#call(assertions, subject) ⇒ Object
Evaluate this expectation against a subject.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/sus/receive.rb', line 140 def call(assertions, subject) assertions.nested(self) do |assertions| mock = @base.mock(subject) called = 0 if call_original? mock.before(@method) do |*arguments, **, &block| called += 1 validate(mock, assertions, arguments, , block) end else mock.replace(@method) do |*arguments, **, &block| called += 1 validate(mock, assertions, arguments, , block) next @returning.call(*arguments, **, &block) end end if @times assertions.defer do @times.call(assertions, called) end end end end |
#call_original? ⇒ Boolean
171 172 173 |
# File 'lib/sus/receive.rb', line 171 def call_original? @returning.nil? end |
#once ⇒ Object
Specify that the method should be called exactly once.
69 70 71 72 |
# File 'lib/sus/receive.rb', line 69 def once @times = Times.new(Be.new(:==, 1)) return self end |
#print(output) ⇒ Object
Print a representation of this expectation.
29 30 31 |
# File 'lib/sus/receive.rb', line 29 def print(output) output.write("receive ", :variable, @method.to_s, :reset) end |
#twice ⇒ Object
Specify that the method should be called exactly twice.
76 77 78 79 |
# File 'lib/sus/receive.rb', line 76 def twice @times = Times.new(Be.new(:==, 2)) return self end |
#validate(mock, assertions, arguments, options, block) ⇒ Object
Validate the method call arguments, options, and block.
127 128 129 130 131 132 133 134 135 |
# File 'lib/sus/receive.rb', line 127 def validate(mock, assertions, arguments, , block) return unless @arguments or @options or @block assertions.nested(self) do |assertions| @arguments.call(assertions, arguments) if @arguments @options.call(assertions, ) if @options @block.call(assertions, block) if @block end end |
#with(*arguments, **options) ⇒ Object
Specify that the method should be called with specific arguments and options.
61 62 63 64 65 |
# File 'lib/sus/receive.rb', line 61 def with(*arguments, **) with_arguments(Be.new(:==, arguments)) if arguments.any? (Be.new(:==, )) if .any? return self end |
#with_arguments(predicate) ⇒ Object
Specify that the method should be called with specific arguments.
36 37 38 39 |
# File 'lib/sus/receive.rb', line 36 def with_arguments(predicate) @arguments = WithArguments.new(predicate) return self end |
#with_block(predicate = Be.new(:!=, nil)) ⇒ Object
Specify that the method should be called with a block.
52 53 54 55 |
# File 'lib/sus/receive.rb', line 52 def with_block(predicate = Be.new(:!=, nil)) @block = WithBlock.new(predicate) return self end |
#with_call_count(predicate) ⇒ Object
Specify a predicate to match against the call count.
84 85 86 87 |
# File 'lib/sus/receive.rb', line 84 def with_call_count(predicate) @times = Times.new(predicate) return self end |
#with_options(predicate) ⇒ Object
Specify that the method should be called with specific keyword options.
44 45 46 47 |
# File 'lib/sus/receive.rb', line 44 def (predicate) @options = WithOptions.new(predicate) return self end |