uber / cadence

Cadence is a distributed, scalable, durable, and highly available orchestration engine to execute asynchronous long-running business logic in a scalable and resilient way.
https://cadenceworkflow.io
MIT License
8.14k stars 786 forks source link

Testing framework does not support .Times(0) on mocks #3148

Open evanfuller opened 4 years ago

evanfuller commented 4 years ago

I am testing a workflow where I call a few activities in sequence. If activity A fails, then the workflow should be aborted, and activity B should not be called. I have written the following test code:

    m.env.OnActivity(m.workflow.ActivityA, mock.Anything, mock.Anything).Return(&myStruct{}, nil)
    m.env.OnActivity(m.workflow.ActivityB, mock.Anything, mock.Anything).Times(0)
    m.env.ExecuteWorkflow(m.workflow.Start, myArg)

When I run this test, I get the following error from the workflow:

    --- FAIL: TestMyWorkflow/TestSecondActivityIsNotCalled (0.45s)
        my_test.go:60: PASS:    github.com/.../workflow.(*MyWorkflow).ActivityA-fm(string,string)
        my_test.go:60: FAIL:    github.com/.../workflow.(*MyWorkflow).ActivityB-fm(string,string)
                    at: [workflow_testsuite.go:225 my_test.go:92]
        my_test.go:60: FAIL: 1 out of 2 expectation(s) were met.
                The code you are testing needs to make 1 more call(s).
                at: [my_test.go:60 suite.go:126 suite.go:134]

Could the mocks be updated to support .Times(0)?

mfateev commented 4 years ago

I believe you want the env.AssertNotCalled exposed. As a workaround use env.SetOnActivityStartedListener interceptor to check that activity of a certain type is not called.

Times(0) is not supported by the underlying testify framework: https://github.com/stretchr/testify/blob/f6cbfc0d0341bd4f04831a275adbef45a2221690/mock/mock.go#L50