3

I have gRPC service (say svc1) which invokes another gRPC service, using its gRPC stub (say svc2_client).

I have generated the client mock for svc2_client. Unit tests as per documentation are working fine, i.e. svc2_client is tested using the generated mock.

However, I have svc1 as shown below:

// Synonymous to proto service generated.
type Svc1 struct {
    rt svc2pb.Svc2Client
}

// Constructor to get Service.
func NewSvc1(rt svc2pb.Svc2Client) *Svc1 {
    return &Svc1{rt}
}
...
...

Expected behavior:
Expecting it to succeed

To Reproduce
Steps to reproduce the behavior:

Returns an error:

=== RUN   TestMethod1Svc1

    TestMethod1Svc1: svc1.go:40: Unexpected call to *mock_svc2client.MockSvc2Client.Method1([context.Background.WithDeadline(2020-06-22 10:14:25.620736 +0530 IST m=+3.002262784 [2.999757626s]) key1:"1"  } ]) at ...server/mocks/svc2client_mock.go:65 because: there are no expected calls of the method "Method1" for that receiver
--- FAIL: TestMethod1Svc1 (0.00s)

Newbie in Go, could you help me out here.

Additional Information:

  • gomock mode (source): source
  • gomock version or git ref: github.com/golang/mock v1.4.3
  • golang version: go version go1.14 darwin/amd64
blackgreen
  • 34,072
  • 23
  • 111
  • 129
nukul
  • 113
  • 1
  • 4
  • 10
  • 1
    Sorry, i was not clear - Working - When client mock is verified by generating test over it. not working - as highlighted above i.e. when a service has to invoke grpc client stub ( which is mocked). unit test over this service is failing, while unit test directly over client stub is working fine. – nukul Jun 22 '20 at 13:00
  • Did you find a way ? let us know here. i am stuck with the same issue. – amolgautam Jul 01 '20 at 02:01

2 Answers2

4

You are trying to call a function that is not mocked yet.

Basically, when you execute a mock_client and call functions within it , you have to make sure that the functions are mocked as well. You have to provide an custom inputs and custom outputs to your mocked function as well.

You can refer to this : gomock-blog

you can see this line crankingCircuit.EXPECT().RotateShaft().Return(nil).Times(2) is telling the mock_client to expect the call of the function RotateShaft

Depending on the API , you will have to change the input arguments and output arguments in your mock function.

amolgautam
  • 897
  • 10
  • 20
1

There can be also this case, I forgot to call the mock() function.

for _, tt := range tests {
    t.Run(tt.name, func(t *testing.T) {
        tt.mock(tt.input)
        s := NewServiceServer(dbObject, queueObject)
        _, err := s.ListConstraint(ctx, tt.input)
        if (err != nil) != tt.isErr {
            t.Errorf("ServiceServer.ListC() error = %v, wantErr %v", err, tt.isErr)
            return
        }

    })
}
infiniteLearner
  • 3,555
  • 2
  • 23
  • 32