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

Experimental: Add mocking integration #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Experimental: Add mocking integration #53

wants to merge 1 commit into from

Conversation

jackkleeman
Copy link
Contributor

See mocking example in examples/ticketreservation/main_test.go

Copy link

Test Results

  7 files  ±0    7 suites  ±0   3m 4s ⏱️ +5s
 45 tests ±0   44 ✅ ±0  1 💤 ±0  0 ❌ ±0 
172 runs  ±0  169 ✅ ±0  3 💤 ±0  0 ❌ ±0 

Results for commit 8025b4e. ± Comparison against base commit 1df62ed.

Comment on lines +17 to +21
mockRand := mocks.NewMockRand(t)

mockRand.EXPECT().UUID().Return(uuid.UUID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}))

mockCtx.EXPECT().Rand().Return(mockRand)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add another helper to reduce this boilerplate a little? I found myself duplicating this a decent bit!

Proposed API:

mockCtx.EXPECT().RandUUID().Return(uuid.Max)

Implementation:

func (_e *MockContext_Expecter) RandUUID() *MockRand_UUID_Call {
	mockRand := &MockRand{}
	_e.Rand().Return(mockRand)
	return mockRand.EXPECT().UUID()
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweak to use t in the initialization:

func (_e *MockContext_Expecter) RandUUID(t *testing.T) *MockRand_UUID_Call {
	mockRand := NewMockRand(t)
	_e.Rand().Return(mockRand)
	return mockRand.EXPECT().UUID()
}

}

// ResultAndReturn is a helper method to mock a typical 'Result' call on a DurablePromise; return a concrete value or an error
func (_e *MockDurablePromise_Expecter) ResultAndReturn(value any, err error) *MockDurablePromise_Result_Call {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice addition as well, makes writing tests for promises much easier! One suggestion specifically for these promises is another helper with initializing them:

func (_e *MockContext_Expecter) PromiseByName(t *testing.T, promiseName string) *MockDurablePromise_Expecter {
	mockPromise := NewMockDurablePromise(t)
	_e.Promise(promiseName, mock.Anything).Return(mockPromise)
	return mockPromise.EXPECT()
}

Usage might look like:

	mockCtx.EXPECT().
		PromiseByName(t, "some_cool_promise").
		ResultAndReturn(neat.Data{Wow: "Amazing"}, nil)

Copy link

@co-go co-go left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the helpers I suggested, they don't need the t param passed into them, but it would be nice. Having the user pass these through params is also not ideal, especially because the mock has already been initialized with t.

I did some digging to see if we could pull that from the mock context itself, but testify/mock keeps it private. So the next best thing might be to alter the generated template from mockery to persist the passed in t into the Mock struct, which currently is hardcoded, but I'd imagine another config var to allow tweaking these templates might be received better in mockery than tweaking a core struct in testify.

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

Successfully merging this pull request may close these issues.

2 participants