stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
22.5k stars 1.56k forks source link

FunctionalOptions dont work if function call not in the test-function scope #1449

Open snirye opened 11 months ago

snirye commented 11 months ago

example from mock_test.go this works:

func Test_Mock_AssertExpectationsFunctionalOptionsType(t *testing.T) {

    var mockedService = new(TestExampleImplementation)

    mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions(OpNum(1), OpStr("foo"))).Return(nil).Once()

    tt := new(testing.T)
    assert.False(t, mockedService.AssertExpectations(tt))

    // make the call now
    mockedService.TheExampleMethodFunctionalOptions("test", OpNum(1), OpStr("foo"))

    // now assert expectations
    assert.True(t, mockedService.AssertExpectations(tt))

}

this doesnt work:


func callTheExampleMethodFunctionalOptionsFromHere(i *TestExampleImplementation) error {
    return i.TheExampleMethodFunctionalOptions("test", OpNum(1), OpStr("foo"))
}

func Test_Mock_AssertExpectationsFunctionalOptionsType(t *testing.T) {

    var mockedService = new(TestExampleImplementation)

    mockedService.On("TheExampleMethodFunctionalOptions", "test", FunctionalOptions(OpNum(1), OpStr("foo"))).Return(nil).Once()

    tt := new(testing.T)
    assert.False(t, mockedService.AssertExpectations(tt))

    // make the call now
    callTheExampleMethodFunctionalOptionsFromHere(mockedService) // change here

    // now assert expectations
    assert.True(t, mockedService.AssertExpectations(tt))

}
tscales commented 10 months ago

The bug is in assertOpts

The issue is that assertOpts is gathering a list of expected function names and actual function names using runtime.FuncForPC and failing the test when they don't match.

The test output hints at this but is cryptic in where these values are coming from.

 1: FAIL:  [mock.callTheExampleMethodFunctionalOptionsFromHere mock.callTheExampleMethodFunctionalOptionsFromHere] != [mock.Test_Mock_AssertExpectationsFunctionalOptionsType mock.Test_Mock_AssertExpectationsFunctionalOptionsType] [recovered]

That's because it actually strips away the function name. Here is what happens when you do a printLn in funcName

funcName: "github.com/stretchr/testify/mock.Test_Mock_AssertExpectationsFunctionalOptionsType.func1"
funcName: "github.com/stretchr/testify/mock.Test_Mock_AssertExpectationsFunctionalOptionsType.func2"
funcName: "github.com/stretchr/testify/mock.callTheExampleMethodFunctionalOptionsFromHere.func1"
funcName: "github.com/stretchr/testify/mock.callTheExampleMethodFunctionalOptionsFromHere.func2"

now it makes a lot more sense why its failing.

tscales commented 10 months ago

looks like this is a duplicate of #1380 and there and #1381 has been proposed to solve it.

pizzqc commented 3 months ago

I have similar issue but funny enough when running the test in debug (no breakpoint) the test works but when ran normally (not in debug) the value changes and test fails. Any idea why?

Would it be related to this thread?