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

Support for mocks in tests #137

Open
chatrasen opened this issue Aug 22, 2020 · 2 comments
Open

Support for mocks in tests #137

chatrasen opened this issue Aug 22, 2020 · 2 comments
Assignees

Comments

@chatrasen
Copy link

Majority of the functions involve calls to functions from external packages which need to be mocked. Adding support for mocks can take gotests to the next level I believe.

e.g. An example function and its generated test can look something like this (pseudocode)

CODE

package A

import B

func fA(in) out{
	...
    resp := B.fB(p1, p2)
    ...
}

TEST

package A

import bmock

func TestfA(t *testing.T) {
	type mockB struct {
		resp   // mock resp of B.fB
		times  // number of times B.fB will be called
	}
	
	testCases := []struct {
		name
		args
		mockB 
		want
	} { // add testcases }

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			gmc := gomock.NewController(t)
			defer gmc.Finish()

			bStub := bmock.NewMockService(t) // just an example, this is debatable
			bstub.Expect().fB(gomock.Any(), gomock.Any()). // 2 params so 2 gomock.Any()
					Times(tc.mockB.times).
					Return(tc.mockB.resp)

			...... // existing logic follows
		})
	}
}

I would first like to ask whether you have already thought about the feasibility of this idea. If you feel this is possible, I would love to work on this enhancement.

@cweill
Copy link
Owner

cweill commented Feb 22, 2021

Hi @chatrasen, while personally I'm not the biggest fan of mocks (I prefer fakes and stubs), I understand that many Go programmers would love to have this feature. I also see that goland/mock is very popular.

I will approve a PR introducing this feature, as long as it is disabled by default, and enabled by a flag like --automock='mock', so that we can extend this to use other mocking libraries in the future.

@johan-lejdung
Copy link

I'm using mockery and are very interested in trying this package out! It would be amazing to get some generated code with support for mockery mocks as well 🙏

Following with great interest

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

No branches or pull requests

3 participants