ovechkin-dm / mockio

Mockito for golang
MIT License
73 stars 2 forks source link

VerifyNoMoreInteractions is not working as expected #34

Closed Zurvarian closed 9 months ago

Zurvarian commented 9 months ago

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 commented 9 months ago

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.

Zurvarian commented 9 months ago

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

Hope it gets enough attention from the Go Devs 😉

ovechkin-dm commented 9 months ago

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 commented 9 months ago

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.