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

Verify after ThenReturn does not seem to work with Never() #83

Closed
jacksehr opened this issue Oct 20, 2024 · 4 comments · Fixed by #84
Closed

Verify after ThenReturn does not seem to work with Never() #83

jacksehr opened this issue Oct 20, 2024 · 4 comments · Fixed by #84
Assignees
Labels
bug Something isn't working

Comments

@jacksehr
Copy link

jacksehr commented Oct 20, 2024

Just double checking if this is a bug or simply not how the API is meant to be used -- the docs explain that the following is available to verify a stub was used correctly:

When(greeter.Greet("John")).ThenReturn("hello world").Verify(Once())

However, if I try to use this with Never(), like so:

WhenSingle(m.MockedMethod()).Verify(Never())

I receive the following error when running the test:

Cause:
    expected num method calls: atLeastOnce, got: 0

This is the case whether I include a .ThenReturn() in the chain or not. Is it intentional that using Verify in this way is not compatible with Never()?

@ovechkin-dm ovechkin-dm self-assigned this Oct 22, 2024
@ovechkin-dm
Copy link
Owner

Unfortunately, I cannot reproduce this behaviour.

func TestVerifyNeverInReturner(t *testing.T) {
	SetUp(t)
	m := Mock[iface]()
	WhenSingle(m.Foo(12)).Verify(Never())
	m.Foo(12)
}

Example above works as expected and fails with error:

Cause:
        	expected num method calls: 0, got : 1
        		iface.Foo(Equal(12))

Can you please provide fully reproducible example of you behaviour?

@jacksehr
Copy link
Author

Ah, sorry, I believe this is due to the use of StrictVerify. As described in its comment:

StrictVerify enables strict verification of mock calls. This means that all mocked methods that are not called will be reported as errors...

This is what adds the atLeastOnce -- which I suppose makes it incompatible with Verify(Never()).

e.g. this passes:

func TestVerifyNeverInReturner(t *testing.T) {
	SetUp(t)
	m := Mock[iface]()
	WhenSingle(m.Foo(12)).Verify(Never())
}

While this fails:

func TestVerifyNeverInReturner(t *testing.T) {
-	SetUp(t)
+	SetUp(t, mockopts.StrictVerify())
	m := Mock[iface]()
	WhenSingle(m.Foo(12)).Verify(Never())
}
...
expected num method calls: atLeastOnce, got: 0
       		iface.Foo(Equal(12))
...

I can see how this might be a bit of an annoying edge case to support when using StrictVerify -- if this is the intentional API I'm happy to close this issue. It would be nice to be able to use both StrictVerify and Never though!

@ovechkin-dm ovechkin-dm added the bug Something isn't working label Oct 28, 2024
@ovechkin-dm ovechkin-dm linked a pull request Oct 28, 2024 that will close this issue
@ovechkin-dm ovechkin-dm reopened this Oct 28, 2024
@ovechkin-dm
Copy link
Owner

ovechkin-dm commented Oct 28, 2024

I see the problem now.
I agree that the behavior should be that test like this succeeds:

func TestVerifyNeverInReturner(t *testing.T) {
	SetUp(t, mockopts.StrictVerify())
	m := Mock[iface]()
	WhenSingle(m.Foo(12)).Verify(Never())
}

The fix is made in v1.0.2
However I would encourage you to use standard api for such cases, because it removes potential ambiguity in test interpretation:

func TestVerifyNeverInReturner(t *testing.T) {
	SetUp(t, mockopts.StrictVerify())
	m := Mock[iface]()
	Verify(m, Never()).Foo(12)
}

Can you please verify that everything is working now, and close the issue?

@jacksehr
Copy link
Author

Looks good, thanks for the quick fix! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants