doukang8949 2019-04-25 01:20
浏览 51
已采纳

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

I am writing a unit test for cadence activity function which is using an UUID to retrieve a contact from the contact service. I wonder what context should I pass into cadence activity.

activity.Register(GetContactActivityFunc)

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

This is testing function.

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)
}

The error message I got:

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

1条回答 默认 最新

  • douzai6337 2019-04-25 01:20
    关注

    Use 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)
    

    Make sure that the activity is registered through activity.Register.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献