testify icon indicating copy to clipboard operation
testify copied to clipboard

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

Open snirye opened this issue 1 year ago • 3 comments

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))

}



snirye avatar Aug 03 '23 08:08 snirye

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 avatar Sep 01 '23 02:09 tscales

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

tscales avatar Sep 01 '23 02:09 tscales

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?

pizzqc avatar Mar 19 '24 19:03 pizzqc