File: request_execution_verifier_spec.rb

package info (click to toggle)
ruby-webmock 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: ruby: 12,829; makefile: 6
file content (208 lines) | stat: -rw-r--r-- 8,714 bytes parent folder | download | duplicates (4)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
require 'spec_helper'

describe WebMock::RequestExecutionVerifier do
  before(:each) do
    @verifier = WebMock::RequestExecutionVerifier.new
    @request_pattern = double(WebMock::RequestPattern, to_s: "www.example.com")
    @verifier.request_pattern = @request_pattern
    allow(WebMock::RequestRegistry.instance).to receive(:to_s).and_return("executed requests")
    @executed_requests_info = "\n\nThe following requests were made:\n\nexecuted requests\n" + "="*60
  end

  describe "description" do

    it "reports the description" do
      @verifier.expected_times_executed = 2
      expect(@verifier.description).to eq "request www.example.com 2 times"
    end

    it "reports description correctly when expectation is 1 time" do
      @verifier.expected_times_executed = 1
      expect(@verifier.description).to eq "request www.example.com 1 time"
    end

    context "at_least_times_executed is set" do
      it "reports description correctly when expectation at least 2 times" do
        @verifier.at_least_times_executed = 2
        expect(@verifier.description).to eq "request www.example.com at least 2 times"
      end

      it "reports description correctly when expectation is at least 3 times" do
        @verifier.at_least_times_executed = 3
        expect(@verifier.description).to eq "request www.example.com at least 3 times"
      end
    end

    context "at_most_times_executed is set" do
      it "reports description correctly when expectation is at most 2 times" do
        @verifier.at_most_times_executed = 2
        expect(@verifier.description).to eq "request www.example.com at most 2 times"
      end

      it "reports description correctly when expectation is at most 1 time" do
        @verifier.at_most_times_executed = 1
        expect(@verifier.description).to eq "request www.example.com at most 1 time"
      end
    end

  end

  describe "failure message" do

    it "reports the failure message" do
      @verifier.times_executed = 0
      @verifier.expected_times_executed = 2
      expected_text = "The request www.example.com was expected to execute 2 times but it executed 0 times"
      expected_text += @executed_requests_info
      expect(@verifier.failure_message).to eq(expected_text)
    end

    it "reports failure message correctly when executed times is one" do
      @verifier.times_executed = 1
      @verifier.expected_times_executed = 1
      expected_text = "The request www.example.com was expected to execute 1 time but it executed 1 time"
      expected_text += @executed_requests_info
      expect(@verifier.failure_message).to eq(expected_text)
    end

    context "at_least_times_executed is set" do
      it "reports failure message correctly when executed times is one" do
        @verifier.times_executed = 1
        @verifier.at_least_times_executed = 2
        expected_text = "The request www.example.com was expected to execute at least 2 times but it executed 1 time"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message).to eq(expected_text)
      end

      it "reports failure message correctly when executed times is two" do
        @verifier.times_executed = 2
        @verifier.at_least_times_executed = 3
        expected_text = "The request www.example.com was expected to execute at least 3 times but it executed 2 times"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message).to eq(expected_text)
      end
    end

    context "at_most_times_executed is set" do
      it "reports failure message correctly when executed times is three" do
        @verifier.times_executed = 3
        @verifier.at_most_times_executed = 2
        expected_text = "The request www.example.com was expected to execute at most 2 times but it executed 3 times"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message).to eq(expected_text)
      end

      it "reports failure message correctly when executed times is two" do
        @verifier.times_executed = 2
        @verifier.at_most_times_executed = 1
        expected_text = "The request www.example.com was expected to execute at most 1 time but it executed 2 times"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message).to eq(expected_text)
      end
    end
  end

  describe "negative failure message" do

    it "reports failure message if it executed number of times specified" do
      @verifier.times_executed = 2
      @verifier.expected_times_executed = 2
      expected_text = "The request www.example.com was not expected to execute 2 times but it executed 2 times"
      expected_text += @executed_requests_info
      expect(@verifier.failure_message_when_negated).to eq(expected_text)
    end

    it "reports failure message when not expected request but it executed" do
      @verifier.times_executed = 1
      expected_text = "The request www.example.com was not expected to execute but it executed 1 time"
      expected_text += @executed_requests_info
      expect(@verifier.failure_message_when_negated).to eq(expected_text)
    end

    context "at_least_times_executed is set" do
      it "reports failure message correctly when executed times is one" do
        @verifier.times_executed = 3
        @verifier.at_least_times_executed = 2
        expected_text = "The request www.example.com was not expected to execute at least 2 times but it executed 3 times"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message_when_negated).to eq(expected_text)
      end

      it "reports failure message correctly when executed times is two" do
        @verifier.times_executed = 2
        @verifier.at_least_times_executed = 2
        expected_text = "The request www.example.com was not expected to execute at least 2 times but it executed 2 times"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message_when_negated).to eq(expected_text)
      end
    end

    context "at_most_times_executed is set" do
      it "reports failure message correctly when executed times is three" do
        @verifier.times_executed = 2
        @verifier.at_most_times_executed = 3
        expected_text = "The request www.example.com was not expected to execute at most 3 times but it executed 2 times"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message_when_negated).to eq(expected_text)
      end

      it "reports failure message correctly when executed times is one" do
        @verifier.times_executed = 1
        @verifier.at_most_times_executed = 2
        expected_text = "The request www.example.com was not expected to execute at most 2 times but it executed 1 time"
        expected_text += @executed_requests_info
        expect(@verifier.failure_message_when_negated).to eq(expected_text)
      end
    end

  end

  describe "matches?" do

    it "succeeds if request was executed expected number of times" do
      expect(WebMock::RequestRegistry.instance).
        to receive(:times_executed).with(@request_pattern).and_return(10)
      @verifier.expected_times_executed = 10
      expect(@verifier.matches?).to be_truthy
    end

    it "fails if request was not executed expected number of times" do
      expect(WebMock::RequestRegistry.instance).
        to receive(:times_executed).with(@request_pattern).and_return(10)
      @verifier.expected_times_executed = 5
      expect(@verifier.matches?).to be_falsey
    end

  end

  describe "does_not_match?" do

    it "fails if request executed expected number of times" do
      expect(WebMock::RequestRegistry.instance).
        to receive(:times_executed).with(@request_pattern).and_return(10)
      @verifier.expected_times_executed = 10
      expect(@verifier.does_not_match?).to be_falsey
    end

    it "succeeds if request was not executed at all and expected number of times was not set" do
      expect(WebMock::RequestRegistry.instance).
        to receive(:times_executed).with(@request_pattern).and_return(0)
      expect(@verifier.does_not_match?).to be_truthy
    end

    it "fails if request was executed and expected number of times was not set" do
      expect(WebMock::RequestRegistry.instance).
        to receive(:times_executed).with(@request_pattern).and_return(1)
      expect(@verifier.does_not_match?).to be_falsey
    end

    it "succeeds if request was not executed expected number of times" do
      expect(WebMock::RequestRegistry.instance).
        to receive(:times_executed).with(@request_pattern).and_return(10)
      @verifier.expected_times_executed = 5
      expect(@verifier.does_not_match?).to be_truthy
    end

  end

end