-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfunction_test.go
54 lines (41 loc) · 1.23 KB
/
mfunction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package serverfull
import (
"context"
"errors"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)
type mInput struct{}
type mOutput struct{}
func testMFunc(ctx context.Context, in mInput) (mOutput, error) { //nolint
return mOutput{}, nil
}
func TestMockingFetcher(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
fn := NewMockFunction(ctrl)
fetcher := NewMockFetcher(ctrl)
mFetcher := &MockingFetcher{
Fetcher: fetcher,
}
fetcher.EXPECT().Fetch(gomock.Any(), gomock.Any()).Return(fn, nil)
fn.EXPECT().Source().Return(testMFunc)
fn.EXPECT().Errors().Return(nil)
mfn, _ := mFetcher.Fetch(context.Background(), "test")
require.IsType(t, testMFunc, mfn.Source()) // ensure the mock is the right signature
res, err := mfn.Invoke(context.Background(), []byte("{}"))
require.NoError(t, err)
require.Equal(t, []byte("{}"), res)
}
func TestMockingFetcherError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
fetcher := NewMockFetcher(ctrl)
mFetcher := &MockingFetcher{
Fetcher: fetcher,
}
fetcher.EXPECT().Fetch(gomock.Any(), gomock.Any()).Return(nil, errors.New("fail"))
_, err := mFetcher.Fetch(context.Background(), "test")
require.Error(t, err)
}