title: Using Two Different Mocks in Go Unit Test
body: <p>I am learning to do unit testing using mocks in Go for the first time, using gomock's mockgen utility. My unit tests work fine except for one of them. The method-under-test has two dependencies: one on a database, and the other on an external service it makes rest api calls to. The mock for the database (mockRepo) works fine in that the method-under-test properly invokes the mock instead of the actual repo code. The mock for the rest client, however, continues to invoke the actual rest client and not the mock code. I can't figure out why. Can someone explain why and help fix?</p>
  
  <p>Here is my unit test:</p>
  
  <pre><code>func TestService_CreateWorkspace(t *testing.T) {
      ts := NewTestService(t)
      defer ts.mockCtrl.Finish()
  
      ts.mockClient.EXPECT().POST(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(&amp;http.Response{StatusCode: 200}, nil)
  
      testWs := TestWorkspaces()["max-ws"]
      ts.mockRepo.EXPECT().Create(testWs).Times(1).Return(&amp;testWs, nil)
  
      ws, err := ts.service.CreateWorkspace(&amp;testWs)
  
      assert.Equal(t, testWs, ws)
      assert.NoError(t, err)
  }
  </code></pre>
  
  <p>Here is the code for NewTestService:</p>
  
  <pre><code>type TestService struct {
      mockCtrl   *gomock.Controller
      mockClient *MockRestClient
      mockRepo   *MockRepository
      service    Service
  }
  
  func NewTestService(t *testing.T) *TestService {
      mockCtrl := gomock.NewController(t)
      mockRepo := NewMockRepository(mockCtrl)
      mockClient := NewMockRestClient(mockCtrl)
      return &amp;TestService{
          mockCtrl:   mockCtrl,
          mockClient: mockClient,
          mockRepo:   mockRepo,
          service:    NewService(mockRepo),
      }
  }
  </code></pre>
  
  <p>Is there an issue with assigning the same mock controller to two different mock objects? Not really sure what's going on here. Any help appreciated.</p>