petergtz / pegomock

Pegomock is a powerful, yet simple mocking framework for the Go programming language
Apache License 2.0
255 stars 28 forks source link

Variadic Issue? not really sure. #112

Open flyinprogrammer opened 3 years ago

flyinprogrammer commented 3 years ago

Context: See the context here: https://github.com/petergtz/pegomock/issues/111

Oh hello again!!

upside_down_sloth


So now I've made a new discovery! I've tried mocking out this fancy PostMessage method in the slack client:

https://github.com/slack-go/slack/blob/master/chat.go#L123

So I wrote an interface and simple wrapper method to mock out something we sort of do in the Atlantis app:

type SlackClient interface {
    PostMessage(channelID string, options ...slack.MsgOption) (respChannel string, respTimestamp string, err error)
}

func MakeSomething(client SlackClient, channelID string, text string) error {
    _, _, err := client.PostMessage(channelID, slack.MsgOptionText(text, false))
    return err
}

And then a simple test to ensure that our slack client receives something we expect:

func TestPostMessage(t *testing.T)  {
    pegomock.RegisterMockTestingT(t)
    mock := mocks.NewMockSlackClient()
    channelID := "something"
    text := "secret message"
    awesomeProject.MakeSomething(mock, channelID, text)
    mock.VerifyWasCalledOnce().PostMessage(channelID, slack.MsgOptionText(text, false))
}

And it goes 💥 with:

--- FAIL: TestPostMessage (0.00s)
    testing_t_support.go:41: 
                ${HOME}/go/pkg/mod/github.com/petergtz/pegomock@v2.8.0+incompatible/testing_t_support.go:40 +0x5e
        github.com/petergtz/pegomock.(*GenericMock).Verify(0xc00000e4a0, 0x0, 0x12cae40, 0xc00007ef00, 0x127a246, 0xb, 0xc00000e500, 0x2, 0x2, 0xc000059ed0, ...)
                ${HOME}/go/pkg/mod/github.com/petergtz/pegomock@v2.8.0+incompatible/dsl.go:153 +0x5be
        awesomeProject/mocks.(*VerifierMockSlackClient).PostMessage(0xc000059f48, 0x1279af1, 0x9, 0xc000059f40, 0x1, 0x1, 0x0)
                ${HOME}/go/src/awesomeProject/mocks/mock_slack_client.go:96 +0x267
        awesomeProject_test.TestPostMessage(0xc000001b00)
                ${HOME}/go/src/awesomeProject/slack_test.go:17 +0x1b7
        testing.tRunner(0xc000001b00, 0x128d170)
                /usr/local/go/src/testing/testing.go:1123 +0xef
        created by testing.(*T).Run
                /usr/local/go/src/testing/testing.go:1168 +0x2b3

        Mock invocation count for PostMessage("something", (slack.MsgOption)(0x11f3f40)) does not match expectation.

                Expected: 1; but got: 0

                But other interactions with this mock were:
                PostMessage("something", (slack.MsgOption)(0x11f3f40))
FAIL
exit status 1
FAIL    awesomeProject  0.284s

Which is quite odd because it was invoked with the signature, but somehow it wasn't?

Here's the example project: https://github.com/flyinprogrammer/pegomock_examples/tree/issue/variadic

I've tried debugging this in GoLand, but unfortunately I am 🥔. I'll keep poking this to learn how this project works, but if someone happens to know a workaround, or is up for teaching me how or what went wrong I'd love to learn.

petergtz commented 3 years ago

Hello again @flyinprogrammer,

So just started looking into this and it is weird, because there's an actual unit test for this: https://github.com/petergtz/pegomock/blob/e07328acdcbfed1c0b19c392a91f30438e093d77/dsl_test.go#L730-L780

So taking a deeper look at it right now.

petergtz commented 3 years ago

Actually, now that I look at it, these tests only use the argument capture mechanism. Need to check why this is the case (sorry, written this 4 years ago 😬 )

petergtz commented 3 years ago

Ah. The crux is here: https://github.com/petergtz/pegomock/blob/6d417a09fb4ca6f21a532c740917bba9fb807972/dsl.go#L371-L374

This logic is wrong and must be fixed. It might not be as simple as removing this check.