Class: Sus::Receive

Inherits:
Object
  • Object
show all
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

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_raiseObject

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, **options, &block|
				called += 1
				
				validate(mock, assertions, arguments, options, block)
			end
		else
			mock.replace(@method) do |*arguments, **options, &block|
				called += 1
				
				validate(mock, assertions, arguments, options, block)
				
				next @returning.call(*arguments, **options, &block)
			end
		end
		
		if @times
			assertions.defer do
				@times.call(assertions, called)
			end
		end
	end
end

#call_original?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/sus/receive.rb', line 171

def call_original?
	@returning.nil?
end

#onceObject

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 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

#twiceObject

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, options, block)
	return unless @arguments or @options or @block
	
	assertions.nested(self) do |assertions|
		@arguments.call(assertions, arguments) if @arguments
		@options.call(assertions, options) 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, **options)
	with_arguments(Be.new(:==, arguments)) if arguments.any?
	with_options(Be.new(:==, options)) if options.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 with_options(predicate)
	@options = WithOptions.new(predicate)
	return self
end