...

Source file src/github.com/golang/mock/gomock/example_test.go

Documentation: github.com/golang/mock/gomock

     1  package gomock_test
     2  
     3  //go:generate mockgen -destination mock_test.go -package gomock_test -source example_test.go
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/golang/mock/gomock"
    11  )
    12  
    13  type Foo interface {
    14  	Bar(string) string
    15  }
    16  
    17  func ExampleCall_DoAndReturn_latency() {
    18  	t := &testing.T{} // provided by test
    19  	ctrl := gomock.NewController(t)
    20  	mockIndex := NewMockFoo(ctrl)
    21  
    22  	mockIndex.EXPECT().Bar(gomock.Any()).DoAndReturn(
    23  		func(arg string) string {
    24  			time.Sleep(1 * time.Millisecond)
    25  			return "I'm sleepy"
    26  		},
    27  	)
    28  
    29  	r := mockIndex.Bar("foo")
    30  	fmt.Println(r)
    31  	// Output: I'm sleepy
    32  }
    33  
    34  func ExampleCall_DoAndReturn_captureArguments() {
    35  	t := &testing.T{} // provided by test
    36  	ctrl := gomock.NewController(t)
    37  	mockIndex := NewMockFoo(ctrl)
    38  	var s string
    39  
    40  	mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn(
    41  		func(arg string) interface{} {
    42  			s = arg
    43  			return "I'm sleepy"
    44  		},
    45  	)
    46  
    47  	r := mockIndex.Bar("foo")
    48  	fmt.Printf("%s %s", r, s)
    49  	// Output: I'm sleepy foo
    50  }
    51  

View as plain text