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 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update user mock to be in test package (#566)
Fixes: #560
- Loading branch information
Showing
7 changed files
with
122 additions
and
99 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
package gomock_test | ||
|
||
//go:generate mockgen -destination mock_test.go -package gomock_test -source example_test.go | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
type Foo interface { | ||
Bar(string) string | ||
} | ||
|
||
func ExampleCall_DoAndReturn_latency() { | ||
t := &testing.T{} // provided by test | ||
ctrl := gomock.NewController(t) | ||
mockIndex := NewMockFoo(ctrl) | ||
|
||
mockIndex.EXPECT().Bar(gomock.Any()).DoAndReturn( | ||
// signature of anonymous function must have the same number of input and output arguments as the mocked method. | ||
func(arg string) string { | ||
time.Sleep(1 * time.Millisecond) | ||
return "I'm sleepy" | ||
}, | ||
) | ||
|
||
r := mockIndex.Bar("foo") | ||
fmt.Println(r) | ||
// Output: I'm sleepy | ||
} | ||
|
||
func ExampleCall_DoAndReturn_captureArguments() { | ||
t := &testing.T{} // provided by test | ||
ctrl := gomock.NewController(t) | ||
mockIndex := NewMockFoo(ctrl) | ||
var s string | ||
|
||
mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn( | ||
// signature of anonymous function must have the same number of input and output arguments as the mocked method. | ||
func(arg string) interface{} { | ||
s = arg | ||
return "I'm sleepy" | ||
}, | ||
) | ||
|
||
r := mockIndex.Bar("foo") | ||
fmt.Printf("%s %s", r, s) | ||
// Output: I'm sleepy foo | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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