Mock Not Recognized In Unit Test

Hello,

I am attempting to test a standard Go (i.e. non-Temporal) method that includes an ExecuteWorkflow call. I want to mock the response of the workflow. I am using a mock client as my Temporal client and using OnWorkflow to mock the ExecuteWorkflow response in the method, but I am still receiving an error when attempting to run the test.

Here is my unit test code:

type UnitTestSuite struct {
	suite.Suite
	testsuite.WorkflowTestSuite

	env *testsuite.TestWorkflowEnvironment
}

func (s *UnitTestSuite) SetupTest() {
	s.env = s.NewTestWorkflowEnvironment()
}

func (s *UnitTestSuite) AfterTest(suiteName, testName string) {
	s.env.AssertExpectations(s.T())
}

func (s *UnitTestSuite) TestHandleGetAccessTokenSuccess() {
	ctx := context.Background()
	mockClient := &mocks.Client{}

	// mocking http request setup, unimportant
	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer([]byte("{\"username\":\"test_username\", \"password\":\"test_password\"}")))

	// workflow mock
	expectedResponse := responses.GetAccessTokenResponse{Token: "token", Type: "bearer"}
	s.env.RegisterWorkflow(workflows.GetAccessTokenWorkflow) // not sure if this is even necessary
	s.env.OnWorkflow(workflows.GetAccessTokenWorkflow, mock.Anything, mock.Anything).Return(
		func(ctx context.Context, value inputs.GetAccessTokenInput) (responses.GetAccessTokenResponse, error) {
			return expectedResponse, nil
		},
	)

	baseHandler := NewBaseHandler(mockClient, "test-task-queue") // some object that includes mockClient as a field
	baseHandler.HandleGetAccessToken(c) // HandleGetAccessToken now has access to mockClient & uses it as its temporal client and calls ExecuteWorkflow on it
}

func TestUnitTestSuite(t *testing.T) {
	suite.Run(t, new(UnitTestSuite))
}

Relevant HandleGetAccessToken code:


func (baseHandler *BaseHandler) HandleGetAccessToken(c *gin.Context) {
........
        workflowRun, err := baseHandler.temporalClient.ExecuteWorkflow(c.Request.Context(), workflowOptions, workflows.GetAccessTokenWorkflow, input) // baseHandler.temporalClient is the mockClient
.......
}

So, HandleGetAccessToken is the method that executes the workflow, on the temporal client object that’s part of the baseHandler object (which in the case of this test, is mocks.Client{} object).

When I run the test, I am receiving the following error output:

assert: mock: I don’t know what to return because the method call was unexpected.

Either do Mock.On(“ExecuteWorkflow”).Return(…) first, or remove the ExecuteWorkflow() call.

This method was unexpected:

ExecuteWorkflow(*context.emptyCtx,internal.StartWorkflowOptions,func(internal.Context, inputs.GetAccessTokenInput) (responses.GetAccessTokenResponse, error),inputs.GetAccessTokenInput)

0: (*context.emptyCtx)(0xc000128008)

1: internal.StartWorkflowOptions{ID:“get_access_token_214dc745-dd90-48cf-8751-d964eb4a04e7”, TaskQueue:“test-task-queue”, WorkflowExecutionTimeout:0, WorkflowRunTimeout:0, WorkflowTaskTimeout:0, WorkflowIDReusePolicy:0, WorkflowExecutionErrorWhenAlreadyStarted:false, RetryPolicy:(*internal.RetryPolicy)(nil), CronSchedule:“”, Memo:map[string]interface {}(nil), SearchAttributes:map[string]interface {}(nil)}

2: (func(internal.Context, inputs.GetAccessTokenInput) (responses.GetAccessTokenResponse, error))(0x1a8ace0)

3: inputs.GetAccessTokenInput{Username:“test_username”, Password:“test_password”, FASHost:“”, FASPort:0x0}

I thought it might be a mismatched mock but I don’t see anything. Any help would be appreciated.
Thanks

Creating a unit test suite to unit test your workflows is unrelated to creating a mock client. You created a mock client and without setting any expectations on the mock tried to call something on it which is why you got “method was unexpected”. If you do expect that method, use the mock client to set those expectations. If you want to unit test the running workflow, that’s what the test suite is for, not for unit testing the calling of a workflow.