-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: main
Are you sure you want to change the base?
Conversation
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) |
There was a problem hiding this comment.
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()
}
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)
There was a problem hiding this 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
.
See mocking example in examples/ticketreservation/main_test.go