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

Functional Options testing broken for indirect calls #1380

Open
dillonstreator opened this issue May 10, 2023 · 2 comments
Open

Functional Options testing broken for indirect calls #1380

dillonstreator opened this issue May 10, 2023 · 2 comments
Labels
bug mock.ArgumentMatcher About matching arguments in mock pkg-mock Any issues related to Mock

Comments

@dillonstreator
Copy link
Contributor

dillonstreator commented May 10, 2023

The changes introduced to support testing of functional options (#1023) are broken with indirect calls.

When the service which exposes the method that supports functional options is injected into another function and invoked, the mock assertions are broken.

The following test and mocks should be supported.

diff --git a/mock/mock_test.go b/mock/mock_test.go
index 60cf5d0..1c7ca47 100644
--- a/mock/mock_test.go
+++ b/mock/mock_test.go
@@ -20,6 +20,7 @@ import (
 // ExampleInterface represents an example interface.
 type ExampleInterface interface {
 	TheExampleMethod(a, b, c int) (int, error)
+	TheExampleMethodFunctionalOptions(x string, opts ...OptionFn) error
 }
 
 // TestExampleImplementation is a test implementation of ExampleInterface
@@ -1455,6 +1456,27 @@ func Test_Mock_AssertExpectationsFunctionalOptionsType(t *testing.T) {
 
 }
 
+func Test_Mock_AssertExpectationsFunctionalOptionsType_indirect(t *testing.T) {
+
+	indirectFunctionalOptions := func(ei ExampleInterface) {
+		ei.TheExampleMethodFunctionalOptions("testing", OpStr("test"), OpNum(1))
+	}
+
+	var mockedService = new(TestExampleImplementation)
+
+	mockedService.On("TheExampleMethodFunctionalOptions", "testing", FunctionalOptions(OpStr("test"), OpNum(1))).Return(nil).Once()
+
+	tt := new(testing.T)
+	assert.False(t, mockedService.AssertExpectations(tt))
+
+	// make the call now
+	indirectFunctionalOptions(mockedService)
+
+	// now assert expectations
+	assert.True(t, mockedService.AssertExpectations(tt))
+
+}
+
 func Test_Mock_AssertExpectationsFunctionalOptionsType_Empty(t *testing.T) {
 
 	var mockedService = new(TestExampleImplementation)

but instead results in the following error:

mock: Unexpected Method Call
-----------------------------

TheExampleMethodFunctionalOptions(string,[]mock.OptionFn)
		0: "testing"
		1: []mock.OptionFn{(mock.OptionFn)(0x11a8020), (mock.OptionFn)(0x11a8000)}

The closest call I have is: 

TheExampleMethodFunctionalOptions(string,*mock.FunctionalOptionsArgument)
		0: "testing"
		1: []mock.OptionFn{(mock.OptionFn)(0x11a0980), (mock.OptionFn)(0x11a0960)}


Diff: 0: PASS:  (string=testing) == (string=testing)
	1: FAIL:  [mock.Test_Mock_AssertExpectationsFunctionalOptionsType_indirect.func1 mock.Test_Mock_AssertExpectationsFunctionalOptionsType_indirect.func1] != [mock.Test_Mock_AssertExpectationsFunctionalOptionsType_indirect mock.Test_Mock_AssertExpectationsFunctionalOptionsType_indirect]

The culprit seems to be the funcName call which is used in assertOpts returning unexpected values.

@PoshAlpaca
Copy link

Thank you! I was running into this issue. Tested your fork and it worked well for me 👌

@dgunay
Copy link

dgunay commented May 5, 2024

Sad to see this never quite make it - I actually decided not to use functional options on a really suitable place for it the other day because of it. Is there any chance we could get #1381 going again?

@dolmen dolmen added the mock.ArgumentMatcher About matching arguments in mock label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mock.ArgumentMatcher About matching arguments in mock pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants