Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Commit bff0032

Browse files
authored
Merge pull request #9 from sandvikcode/allow-for-0-times
Allow for mock server to verify that a call was done 0 times.
2 parents 05ca02d + 925d632 commit bff0032

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

pkg/mockclient/expectations.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type ResponseBody struct {
3737
// Times defines how many times the MockServer will serve a given request in expectation mode whilst
3838
// in verification mode defines the expected number of calls
3939
type Times struct {
40-
AtLeast int `json:"atLeast,omitempty"` // valid for verifications only
41-
AtMost int `json:"atMost,omitempty"` // valid for verifications only
40+
AtLeast *int `json:"atLeast,omitempty"` // valid for verifications only
41+
AtMost *int `json:"atMost,omitempty"` // valid for verifications only
4242
RemainingTimes int `json:"remainingTimes,omitempty"` // valid for expectations only
4343
Unlimited *bool `json:"unlimited,omitempty"` // valid for expectations only
4444
}
@@ -134,7 +134,7 @@ func WhenTimes(times int) ExpectationOption {
134134
return func(e *Expectation) *Expectation {
135135
e.Times = &Times{
136136
RemainingTimes: times,
137-
Unlimited: newBool(false),
137+
Unlimited: boolPointer(false),
138138
}
139139
return e
140140
}
@@ -196,7 +196,7 @@ func ThenResponseDelay(delay time.Duration) ExpectationOption {
196196
}
197197
}
198198

199-
func newBool(value bool) *bool {
200-
b := value
201-
return &b
199+
func boolPointer(value bool) *bool {
200+
b := value
201+
return &b
202202
}

pkg/mockclient/verifications.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func CreateVerification(opts ...ExpectationOption) *Expectation {
1313
Path: "/(.*)",
1414
},
1515
Times: &Times{
16-
AtLeast: 1,
17-
AtMost: 1,
16+
AtLeast: integerPointer(1),
17+
AtMost: integerPointer(1),
1818
},
1919
}
2020
// Append all options that are set (discard defaults)
@@ -28,15 +28,15 @@ func CreateVerification(opts ...ExpectationOption) *Expectation {
2828
// ThenAtLeastCalls creates a verification that a matching call was received at least x times by MockServer
2929
func ThenAtLeastCalls(times int) ExpectationOption {
3030
return func(v *Expectation) *Expectation {
31-
v.Times.AtLeast = times
31+
v.Times.AtLeast = integerPointer(times)
3232
return v
3333
}
3434
}
3535

3636
// ThenAtMostCalls creates a verification that a matching call was received at most x times by MockServer
3737
func ThenAtMostCalls(times int) ExpectationOption {
3838
return func(v *Expectation) *Expectation {
39-
v.Times.AtMost = times
39+
v.Times.AtMost = integerPointer(times)
4040
return v
4141
}
4242
}
@@ -63,3 +63,7 @@ func VerifyPath(path string) VerificationOption {
6363
}
6464
}
6565
*/
66+
67+
func integerPointer(i int) *int {
68+
return &i
69+
}

pkg/mockclient/verifications_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ func TestVerifications(t *testing.T) {
2828
"atMost": 1
2929
}
3030
}`},
31-
{"Verify the MockServer was called at least 0 times, and at most 1 times, for a given path, by using the default atMost.", CreateVerification(WhenRequestPath("/path"), ThenAtLeastCalls(1)), `
31+
{"Verify the MockServer was called at least 0 times, and at most 1 times, for a given path, by using the default atMost.", CreateVerification(WhenRequestPath("/path"), ThenAtLeastCalls(0)), `
3232
{
3333
"httpRequest": {
3434
"path": "/path"
3535
},
3636
"times": {
37-
"atLeast": 1,
37+
"atLeast": 0,
3838
"atMost": 1
3939
}
4040
}`},

0 commit comments

Comments
 (0)