-
Notifications
You must be signed in to change notification settings - Fork 553
/
request_execution_verifier.rb
78 lines (64 loc) · 2.27 KB
/
request_execution_verifier.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module WebMock
class RequestExecutionVerifier
attr_accessor :request_pattern, :expected_times_executed, :times_executed, :at_least_times_executed, :at_most_times_executed
def initialize(request_pattern = nil, expected_times_executed = nil, at_least_times_executed = nil, at_most_times_executed = nil)
@request_pattern = request_pattern
@expected_times_executed = expected_times_executed
@at_least_times_executed = at_least_times_executed
@at_most_times_executed = at_most_times_executed
end
def matches?
@times_executed =
RequestRegistry.instance.times_executed(@request_pattern)
if @at_least_times_executed
@times_executed >= @at_least_times_executed
elsif @at_most_times_executed
@times_executed <= @at_most_times_executed
else
@times_executed == (@expected_times_executed || 1)
end
end
def does_not_match?
@times_executed =
RequestRegistry.instance.times_executed(@request_pattern)
if @expected_times_executed
@times_executed != @expected_times_executed
else
@times_executed == 0
end
end
def description
"request #{request_pattern} #{quantity_phrase.strip}"
end
def failure_message
failure_message_phrase(false)
end
def failure_message_when_negated
failure_message_phrase(true)
end
def self.executed_requests_message
"\n\nThe following requests were made:\n\n#{RequestRegistry.instance}\n" + "="*60
end
private
def failure_message_phrase(is_negated=false)
negation = is_negated ? "was not" : "was"
text = "The request #{request_pattern} #{negation} expected to execute #{quantity_phrase(is_negated)}but it executed #{times(times_executed)}"
text << self.class.executed_requests_message
text
end
def quantity_phrase(is_negated=false)
if @at_least_times_executed
"at least #{times(@at_least_times_executed)} "
elsif @at_most_times_executed
"at most #{times(@at_most_times_executed)} "
elsif @expected_times_executed
"#{times(@expected_times_executed)} "
else
is_negated ? "" : "#{times(1)} "
end
end
def times(times)
"#{times} time#{ (times == 1) ? '' : 's'}"
end
end
end