How to use TestDoAndReturnSignature method of user_test Package

Best Mock code snippet using user_test.TestDoAndReturnSignature

Run Mock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

user_test.go

Source: user_test.go Github

copy
1// A test that uses a mock.
2package user_test
3
4import (
5	"testing"
6
7	"github.com/golang/mock/gomock"
8	user "github.com/golang/mock/sample"
9	"github.com/golang/mock/sample/imp1"
10)
11
12func TestRemember(t *testing.T) {
13	ctrl := gomock.NewController(t)
14	defer ctrl.Finish()
15
16	mockIndex := NewMockIndex(ctrl)
17	mockIndex.EXPECT().Put("a", 1)            // literals work
18	mockIndex.EXPECT().Put("b", gomock.Eq(2)) // matchers work too
19
20	// NillableRet returns error. Not declaring it should result in a nil return.
21	mockIndex.EXPECT().NillableRet()
22	// Calls that returns something assignable to the return type.
23	boolc := make(chan bool)
24	// In this case, "chan bool" is assignable to "chan<- bool".
25	mockIndex.EXPECT().ConcreteRet().Return(boolc)
26	// In this case, nil is assignable to "chan<- bool".
27	mockIndex.EXPECT().ConcreteRet().Return(nil)
28
29	// Should be able to place expectations on variadic methods.
30	mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3) // direct args
31	tri := []interface{}{1, 3, 6, 10, 15}
32	mockIndex.EXPECT().Ellip("%d", tri...) // args from slice
33	mockIndex.EXPECT().EllipOnly(gomock.Eq("arg"))
34
35	user.Remember(mockIndex, []string{"a", "b"}, []interface{}{1, 2})
36	// Check the ConcreteRet calls.
37	if c := mockIndex.ConcreteRet(); c != boolc {
38		t.Errorf("ConcreteRet: got %v, want %v", c, boolc)
39	}
40	if c := mockIndex.ConcreteRet(); c != nil {
41		t.Errorf("ConcreteRet: got %v, want nil", c)
42	}
43
44	// Try one with an action.
45	calledString := ""
46	mockIndex.EXPECT().Put(gomock.Any(), gomock.Any()).Do(func(key string, _ interface{}) {
47		calledString = key
48	})
49	mockIndex.EXPECT().NillableRet()
50	user.Remember(mockIndex, []string{"blah"}, []interface{}{7})
51	if calledString != "blah" {
52		t.Fatalf(`Uh oh. %q != "blah"`, calledString)
53	}
54
55	// Use Do with a nil arg.
56	mockIndex.EXPECT().Put("nil-key", gomock.Any()).Do(func(key string, value interface{}) {
57		if value != nil {
58			t.Errorf("Put did not pass through nil; got %v", value)
59		}
60	})
61	mockIndex.EXPECT().NillableRet()
62	user.Remember(mockIndex, []string{"nil-key"}, []interface{}{nil})
63}
64
65func TestVariadicFunction(t *testing.T) {
66	ctrl := gomock.NewController(t)
67	defer ctrl.Finish()
68
69	mockIndex := NewMockIndex(ctrl)
70	mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) {
71		sum := 0
72		for _, value := range nums {
73			sum += value
74		}
75		if sum != 26 {
76			t.Errorf("Expected 26, got %d", sum)
77		}
78	})
79	mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
80		sum := 0
81		for _, value := range nums {
82			sum += value
83		}
84		if sum != 10 {
85			t.Errorf("Expected 10, got %d", sum)
86		}
87	})
88	mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
89		sum := 0
90		for _, value := range nums {
91			sum += value
92		}
93		if sum != 0 {
94			t.Errorf("Expected 0, got %d", sum)
95		}
96	})
97	mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
98		sum := 0
99		for _, value := range nums {
100			sum += value
101		}
102		if sum != 0 {
103			t.Errorf("Expected 0, got %d", sum)
104		}
105	})
106	mockIndex.EXPECT().Ellip("%d").Do(func(format string, nums ...int) {
107		sum := 0
108		for _, value := range nums {
109			sum += value
110		}
111		if sum != 0 {
112			t.Errorf("Expected 0, got %d", sum)
113		}
114	})
115
116	mockIndex.Ellip("%d", 1, 2, 3, 4) // Match second matcher.
117	mockIndex.Ellip("%d", 5, 6, 7, 8) // Match first matcher.
118	mockIndex.Ellip("%d", 0)
119	mockIndex.Ellip("%d")
120	mockIndex.Ellip("%d")
121}
122
123func TestGrabPointer(t *testing.T) {
124	ctrl := gomock.NewController(t)
125	defer ctrl.Finish()
126
127	mockIndex := NewMockIndex(ctrl)
128	mockIndex.EXPECT().Ptr(gomock.Any()).SetArg(0, 7) // set first argument to 7
129
130	i := user.GrabPointer(mockIndex)
131	if i != 7 {
132		t.Errorf("Expected 7, got %d", i)
133	}
134}
135
136func TestEmbeddedInterface(t *testing.T) {
137	ctrl := gomock.NewController(t)
138	defer ctrl.Finish()
139
140	mockEmbed := NewMockEmbed(ctrl)
141	mockEmbed.EXPECT().RegularMethod()
142	mockEmbed.EXPECT().EmbeddedMethod()
143	mockEmbed.EXPECT().ForeignEmbeddedMethod()
144
145	mockEmbed.RegularMethod()
146	mockEmbed.EmbeddedMethod()
147	var emb imp1.ForeignEmbedded = mockEmbed // also does interface check
148	emb.ForeignEmbeddedMethod()
149}
150
151func TestExpectTrueNil(t *testing.T) {
152	// Make sure that passing "nil" to EXPECT (thus as a nil interface value),
153	// will correctly match a nil concrete type.
154	ctrl := gomock.NewController(t)
155	defer ctrl.Finish()
156
157	mockIndex := NewMockIndex(ctrl)
158	mockIndex.EXPECT().Ptr(nil) // this nil is a nil interface{}
159	mockIndex.Ptr(nil)          // this nil is a nil *int
160}
161
162func TestDoAndReturnSignature(t *testing.T) {
163	t.Run("wrong number of return args", func(t *testing.T) {
164		ctrl := gomock.NewController(t)
165		defer ctrl.Finish()
166
167		mockIndex := NewMockIndex(ctrl)
168
169		mockIndex.EXPECT().Slice(gomock.Any(), gomock.Any()).DoAndReturn(
170			func(_ []int, _ []byte) {},
171		)
172
173		defer func() {
174			if r := recover(); r == nil {
175				t.Error("expected panic")
176			}
177		}()
178
179		mockIndex.Slice([]int{0}, []byte("meow"))
180	})
181
182	t.Run("wrong type of return arg", func(t *testing.T) {
183		ctrl := gomock.NewController(t)
184		defer ctrl.Finish()
185
186		mockIndex := NewMockIndex(ctrl)
187
188		mockIndex.EXPECT().Slice(gomock.Any(), gomock.Any()).DoAndReturn(
189			func(_ []int, _ []byte) bool {
190				return true
191			})
192
193		mockIndex.Slice([]int{0}, []byte("meow"))
194	})
195}
196
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger TestDoAndReturnSignature code on LambdaTest Cloud Grid

Execute automation tests with TestDoAndReturnSignature on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)