Uber Cadence活动的单元测试上下文

时间:2019-04-25 01:20:28

标签: unit-testing go cadence-workflow

我正在编写对节奏活动功能的单元测试,该功能正在使用UUID从联系人服务中检索联系人。我想知道我应该将什么环境纳入节奏活动中。

activity.Register(GetContactActivityFunc)

func GetContactActivityFunc(ctx context.Context, input ContactBbInput) (ContactBbOutput, error) {
   ...
}

这是测试功能。

func TestGetContactActivityFunc(t *testing.T) {
    mockCSInterface := &mocks.Interface{}
    csClient := outbound.NewContactServiceClient(mockCSInterface)
    ctx := context.Background()
    ctx = context.WithValue(ctx, outbound.ContactServiceClientKey, csClient)
    contactUUID := contact.UUID("917801ab-36ff-4eea-8352-d6eafedb5106")
    bbInput := ContactBbInput{
        ContactID: &contactUUID,
    }
    bbOut, err := GetContactActivityFunc(ctx, bbInput)
    assert.NoError(t, err)
    assert.NotNil(t, bbOut.ContactObj)
}

我收到的错误消息:

--- FAIL: TestGetContactActivityFunc (0.00s)
panic: getActivityEnv: Not an activity context [recovered]
    panic: getActivityEnv: Not an activity context

1 个答案:

答案 0 :(得分:1)

使用TestActivityEnvironment

import (
    "go.uber.org/cadence/testsuite"
    "go.uber.org/cadence/worker" 
)

s := &testsuite.WorkflowTestSuite{} 
env := s.NewTestActivityEnvironment() 
// This is needed if ctx contains some external dependencies like
// database client
env.SetWorkerOptions(worker.Options{    
  BackgroundActivityContext: ctx, 
})
contact, err := env.ExecuteActivity(GetContactActivityFunc)

确保活动是通过activity.Register注册的。