1

我正在为使用 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
4

1 回答 1

3

使用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

于 2019-04-25T01:20:27.607 回答