Skip to content

Show call stack where mock was declared on fail #269

Description

@rusinov-artem

Requested feature
Sometime i want to see the call stack to my mock declaration instead of a single file:line

Why the feature is needed
Some times i need to name my mock declarations under a helper function to reuse them with parameters. And when i do it, i dont see clearly where test call it. I see only the declaration, but not file:line in a test

(Optional) Proposed solution
I want to add new method WithStack(lvl int) to the Call struct, which will modify
Call.origin parameter, so i can see something like this
my_test.go:132
myhelper.go:42
in it

Let me show my test example

package main

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"go.uber.org/mock/gomock"
)

func Test_Alert(t *testing.T) {
	ctrl := gomock.NewController(t)
	m := NewMockMessanger(ctrl)
	alert := NewAlert(m).WithInfo("info")

	messageWillBeSentSuccessfuly(m)

	err := alert.Notify()
	assert.NoError(t, err)
}

func messageWillBeSentSuccessfuly(m *MockMessanger) {
	m.EXPECT().SendMessage("invalid msg").Return(nil).WithStack(2)
}

in this case i will get an error

=== RUN   Test_Alert
    service.go:24: Unexpected call to *main.MockMessanger.SendMessage([info]) at service.go:24 because:
        expected call at main_test.go:23 doesn't match the argument at index 0.
        Got: info (string)
        Want: is equal to invalid msg (string)
    controller.go:97: missing call(s) to *main.MockMessanger.SendMessage(is equal to invalid msg (string)) main_test.go:23
    controller.go:97: aborting test due to missing call(s)
--- FAIL: Test_Alert (0.00s)
FAIL
FAIL    github.com/rusinov-artem/go-exercise/e00012     0.006s
FAIL
 

But i want

 === RUN   Test_Alert
    service.go:24: Unexpected call to *main.MockMessanger.SendMessage([info]) at /service.go:24 because:
        expected call at
        main_test.go:15
        main_test.go:22
         doesn't match the argument at index 0.
        Got: info (string)
        Want: is equal to invalid msg (string)
    controller.go:97: missing call(s) to *main.MockMessanger.SendMessage(is equal to invalid msg (string))
        main_test.go:15
        main_test.go:22
    controller.go:97: aborting test due to missing call(s)
--- FAIL: Test_Alert (0.00s)
FAIL
FAIL    github.com/rusinov-artem/go-exercise/e00012     0.004s
FAIL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions