Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
snirye opened this issue Aug 3, 2023 · 3 comments
Open
Labels

Comments

@snirye
Copy link
Contributor

snirye commented Aug 3, 2023

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
Copy link
Contributor

tscales commented Sep 1, 2023

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
Copy link
Contributor

tscales commented Sep 1, 2023

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

@pizzqc
Copy link

pizzqc commented Mar 19, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants