How to use ForeignEmbeddedMethod method of user_test Package

Best Mock code snippet using user_test.ForeignEmbeddedMethod

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	"code.google.com/p/gomock/gomock"
8	"code.google.com/p/gomock/sample"
9	"code.google.com/p/gomock/sample/imp1"
10	mock_user "code.google.com/p/gomock/sample/mock_user"
11)
12
13func TestRemember(t *testing.T) {
14	ctrl := gomock.NewController(t)
15	defer ctrl.Finish()
16
17	mockIndex := mock_user.NewMockIndex(ctrl)
18	mockIndex.EXPECT().Put("a", 1)            // literals work
19	mockIndex.EXPECT().Put("b", gomock.Eq(2)) // matchers work too
20
21	// NillableRet returns error. Not declaring it should result in a nil return.
22	mockIndex.EXPECT().NillableRet()
23	// Calls that returns something assignable to the return type.
24	boolc := make(chan bool)
25	// In this case, "chan bool" is assignable to "chan<- bool".
26	mockIndex.EXPECT().ConcreteRet().Return(boolc)
27	// In this case, nil is assignable to "chan<- bool".
28	mockIndex.EXPECT().ConcreteRet().Return(nil)
29
30	// Should be able to place expectations on variadic methods.
31	mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3) // direct args
32	tri := []interface{}{1, 3, 6, 10, 15}
33	mockIndex.EXPECT().Ellip("%d", tri...) // args from slice
34	mockIndex.EXPECT().EllipOnly(gomock.Eq("arg"))
35
36	user.Remember(mockIndex, []string{"a", "b"}, []interface{}{1, 2})
37	// Check the ConcreteRet calls.
38	if c := mockIndex.ConcreteRet(); c != boolc {
39		t.Errorf("ConcreteRet: got %v, want %v", c, boolc)
40	}
41	if c := mockIndex.ConcreteRet(); c != nil {
42		t.Errorf("ConcreteRet: got %v, want nil", c)
43	}
44
45	// Try one with an action.
46	calledString := ""
47	mockIndex.EXPECT().Put(gomock.Any(), gomock.Any()).Do(func(key string, _ interface{}) {
48		calledString = key
49	})
50	mockIndex.EXPECT().NillableRet()
51	user.Remember(mockIndex, []string{"blah"}, []interface{}{7})
52	if calledString != "blah" {
53		t.Fatalf(`Uh oh. %q != "blah"`, calledString)
54	}
55}
56
57func TestGrabPointer(t *testing.T) {
58	ctrl := gomock.NewController(t)
59	defer ctrl.Finish()
60
61	mockIndex := mock_user.NewMockIndex(ctrl)
62	mockIndex.EXPECT().Ptr(gomock.Any()).SetArg(0, 7) // set first argument to 7
63
64	i := user.GrabPointer(mockIndex)
65	if i != 7 {
66		t.Errorf("Expected 7, got %d", i)
67	}
68}
69
70func TestEmbeddedInterface(t *testing.T) {
71	ctrl := gomock.NewController(t)
72	defer ctrl.Finish()
73
74	mockEmbed := mock_user.NewMockEmbed(ctrl)
75	mockEmbed.EXPECT().RegularMethod()
76	mockEmbed.EXPECT().EmbeddedMethod()
77	mockEmbed.EXPECT().ForeignEmbeddedMethod()
78
79	mockEmbed.RegularMethod()
80	mockEmbed.EmbeddedMethod()
81	var emb imp1.ForeignEmbedded = mockEmbed // also does interface check
82	emb.ForeignEmbeddedMethod()
83}
84
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 ForeignEmbeddedMethod code on LambdaTest Cloud Grid

Execute automation tests with ForeignEmbeddedMethod 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)