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

VerifyNoMoreInteractions is not working as expected #34

Closed
Zurvarian opened this issue Jan 3, 2024 · 4 comments · Fixed by #35
Closed

VerifyNoMoreInteractions is not working as expected #34

Zurvarian opened this issue Jan 3, 2024 · 4 comments · Fixed by #35
Assignees
Labels
bug Something isn't working

Comments

@Zurvarian
Copy link

First, kudos for this great library, I come from Java world and, honestly, it has been quite a bummer how bad is mocking in Go...

Now, I've been evaluating your library to introduce it in our stack, and while doing so I've found that the behaviour of VerifyNoMoreInteractions is not doing what the GoDoc says it does.

The GoDoc says:

// VerifyNoMoreInteractions verifies that there are no more unverified interactions with the mock object.
//
// Example usage:
//
//	// Create a mock object for testing
//	mockObj := Mock[MyInterface]()
//
//	// Call the method
//	mockObj.MyMethod()
//
//	// Verify that MyMethod was called exactly once
//	Verify(mockObj, Once()).MyMethod()
//
//	// Verify that there are no more unverified interactions
//	VerifyNoMoreInteractions(mockObj)

But when I've tried exactly that, the test is not failing as expected. Moreover, I've downloaded this project and tweaked the test that is verifying the VerifyNoMoreInteractions method to follow this exact GoDoc (i.e. I've added a verify(m, Once()) and I could see the same behaviour.

func TestNoMoreInteractions(t *testing.T) {
	r := common.NewMockReporter(t)
	SetUp(r)
	m := Mock[iface]()
	WhenSingle(m.Foo(Any[int]())).ThenReturn(10)
	m.Foo(10)
	Verify(m, Once()).Foo(Any[int]())
	VerifyNoMoreInteractions(m)
	r.AssertError()
}

When I run this, I get: ../mockio/tests/verify/common.go:40: Expected error, got nothing

@ovechkin-dm
Copy link
Owner

ovechkin-dm commented Jan 5, 2024

Hello and thank you for the feedback.
Regarding this issue, there's been a misinterpretation of how verifyNoMoreInteractions should work from my side, so I will fix it soon.
However I also see that your example should indeed succeed.
From what I see in mockito, this test should fail:

@Test
    void verificationTest() {
        ExampleInterface exampleInterface = mock(ExampleInterface.class);
        exampleInterface.Foo1(1);
        exampleInterface.Foo2(12);
        verify(exampleInterface, atLeastOnce()).Foo1(1);
        verifyNoMoreInteractions(exampleInterface);
    }

And this test should pass:

@Test
    void verificationTest() {
        ExampleInterface exampleInterface = mock(ExampleInterface.class);
        exampleInterface.Foo1(1);
        verify(exampleInterface, atLeastOnce()).Foo1(1);
        verifyNoMoreInteractions(exampleInterface);
    }

So I will fix the current bug by replicating mockito behaviour.

@ovechkin-dm ovechkin-dm self-assigned this Jan 5, 2024
@ovechkin-dm ovechkin-dm added the bug Something isn't working label Jan 5, 2024
@Zurvarian
Copy link
Author

Thanks!, really love to see this lib in Golang.

Hope it gets enough attention from the Go Devs 😉

@ovechkin-dm ovechkin-dm linked a pull request Jan 5, 2024 that will close this issue
@ovechkin-dm ovechkin-dm reopened this Jan 5, 2024
@ovechkin-dm
Copy link
Owner

ovechkin-dm commented Jan 5, 2024

Done, the behavior of this check is now aligned with mockito.
Could you please check and confirm?
Fix version is v0.4.8
Keep in mind that error description is now not very verbose and will be fixed here

@Zurvarian
Copy link
Author

Checked, and now it is working as expected.

However I've found another, much more subtle issue. I'm opening another ticket to not mix things up.

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