This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for Call.Do and Call.DoAndReturn
fixes issue #469
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package gomock_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
mock_sample "github.com/golang/mock/sample/mock_user" | ||
) | ||
|
||
func ExampleCall_DoAndReturn() { | ||
t := &testing.T{} // provided by test | ||
ctrl := gomock.NewController(t) | ||
mockIndex := mock_sample.NewMockIndex(ctrl) | ||
|
||
mockIndex.EXPECT().Get(gomock.Any()).DoAndReturn( | ||
func() string { | ||
time.Sleep(1 * time.Second) | ||
return "I'm sleepy" | ||
}, | ||
) | ||
} | ||
|
||
func ExampleCall_DoAndReturn_captureArguments() { | ||
t := &testing.T{} // provided by test | ||
ctrl := gomock.NewController(t) | ||
mockIndex := mock_sample.NewMockIndex(ctrl) | ||
var s string | ||
|
||
mockIndex.EXPECT().Get(gomock.AssignableToTypeOf(s)).DoAndReturn( | ||
// When capturing arguments the anonymous function should have the same signature as the mocked method. | ||
func(arg string) interface{} { | ||
time.Sleep(1 * time.Second) | ||
fmt.Println(arg) | ||
return "I'm sleepy" | ||
}, | ||
) | ||
} | ||
|
||
func ExampleCall_Do() { | ||
t := &testing.T{} // provided by test | ||
ctrl := gomock.NewController(t) | ||
mockIndex := mock_sample.NewMockIndex(ctrl) | ||
|
||
mockIndex.EXPECT().Anon(gomock.Any()).Do( | ||
func() { | ||
time.Sleep(1 * time.Second) | ||
}, | ||
) | ||
} | ||
|
||
func ExampleCall_Do_captureArguments() { | ||
t := &testing.T{} // provided by test | ||
ctrl := gomock.NewController(t) | ||
mockIndex := mock_sample.NewMockIndex(ctrl) | ||
|
||
var s string | ||
mockIndex.EXPECT().Anon(gomock.AssignableToTypeOf(s)).Do( | ||
// When capturing arguments the anonymous function should have the same signature as the mocked method. | ||
func(arg string) { | ||
time.Sleep(1 * time.Second) | ||
fmt.Println(arg) | ||
}, | ||
) | ||
} |