Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: verify HasNotBeenCalled #10

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ func (verifier *CallVerifier) HasBeenCalled(expectedCallsCount int) []*Invocatio
func (verifier *CallVerifier) HasBeenCalledOnce() *Invocation {
return verifier.HasBeenCalled(1)[0]
}

// HasNotBeenCalled asserts that no HTTP call has been made.
func (verifier *CallVerifier) HasNotBeenCalled() {
_ = verifier.HasBeenCalled(0)
}
38 changes: 38 additions & 0 deletions verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,41 @@ func TestVerifyingSingleInvocationReturnsThePerformedCall(t *testing.T) {
WithStringPayload("Hello").
WithHeader("Content-Type", "text/plain")
}

func TestVerifyingNoInvocationPasses(t *testing.T) {
t.Parallel()
// Arrange
testState := NewTestingMock(t)
mockedAPI := API(testState)
defer func() { mockedAPI.Close() }()

mockedAPI.
Stub(http.MethodGet, "/endpoint").
WithStatusCode(http.StatusOK)

// Act
mockedAPI.Verify(http.MethodGet, "/endpoint").HasNotBeenCalled()

// Assert
testState.assertDidNotFailed()
}

func TestVerifyingNoInvocationFails(t *testing.T) {
t.Parallel()
// Arrange
testState := NewTestingMock(t)
mockedAPI := API(testState)
defer func() { mockedAPI.Close() }()

mockedAPI.
Stub(http.MethodGet, "/endpoint").
WithStatusCode(http.StatusOK)

// Act
client := http.Client{}
_, _ = client.Get(mockedAPI.GetURL().String() + "/endpoint")
mockedAPI.Verify(http.MethodGet, "/endpoint").HasNotBeenCalled()

// Assert
testState.assertFailedWithFatal()
}