Test workflow was started

I’m attempting to write a unit test to confirm a workflow is started.

Signature of function under test:
func ViewSummaryWorkflow(context workflow.Context, personId int64) (Summary, error)

func (testSuite *ViewSummaryTestSuite) TestViewSummaryInvokesWorkflow() {
	personId := int64(123)
	expected := workflows.Summary{PersonId: personId, Name: "Sam Doe"}
	testSuite.env.RegisterWorkflow(workflows.ViewSummaryWorkflow)
	testSuite.env.OnWorkflow(workflows.ViewSummaryWorkflow, mock.Anything, personId).Return(expected, nil)
	temporal, _ := client.NewClient(client.Options{})
	actual := workflows.ViewSummary(temporal, strconv.FormatInt(personId, 10))
	testSuite.Equal(expected, actual)
}

type ViewSummaryTestSuite struct {
	suite.Suite
	testsuite.WorkflowTestSuite
	env *testsuite.TestWorkflowEnvironment
}

func TestViewSummaryTestSuite(t *testing.T) {
	suite.Run(t, new(ViewSummaryTestSuite))
}

func (testSuite *ViewSummaryTestSuite) SetupTest() {
	testSuite.env = testSuite.NewTestWorkflowEnvironment()
}

func (testSuite *ViewSummaryTestSuite) AfterTest(suiteName, testName string) {
	testSuite.env.AssertExpectations(testSuite.T())
}

AssertExpectations failed with:
FAIL: 0 out of 1 expectation(s) were met.
The code you are testing needs to make 1 more call(s).

I’ve verified that the expected value is returned. What am I missing?

Thank you in advanced.

client.NewClient creates a new client that is completely disconnected from the TestWorkflowEnvironment. Call TestWorkflowEnvironment.ExecuteWorkflow to execute workflows under unit test.

1 Like

Thank you for the reply, maxim. I want to test that a function under test executes the workflow. It’s not the workflow itself that’s under test. I suspect the Temporal testing capabilities are not the right tool for the job and I should either pass a mock client or a mock abstraction of the client.

In this case use the provided Client mock.

2 Likes

Thank you, that’s what I needed. My tests are happy.

1 Like