我正在编写对节奏活动功能的单元测试,该功能正在使用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
答案 0 :(得分:1)
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
注册的。