【问题标题】:Create unit test case for durable function activity in nodejs在nodejs中为持久功能活动创建单元测试用例
【发布时间】:2022-01-08 20:58:44
【问题描述】:

我正在尝试使用 nodejs 为持久功能活动创建单元测试。但是没有找到任何关于那个或任何东西的文件。我缺乏的地方是我不知道在创建测试用例时要通过什么上下文。任何帮助将不胜感激。提前谢谢!!!

module.exports   = {
    // log: jest.fn(),
    log: (function () {
      let main = <any>jest.fn((message) => message);
      let info = jest.fn((message) => message);
      main.info = info;
      let error = jest.fn((message) => message);
      main.error = error
      return main;
    })(),
    bindingData: (function () {
      let main = <any>jest.fn((message) => message);
      let info = jest.fn((message) => message);
      main.info = info;
      let error = jest.fn((message) => message);
      main.error = error;
      return main;
    })()
  };

我正在传递此上下文,但收到与 context.bindingData 相关的错误

【问题讨论】:

    标签: javascript node.js azure azure-functions azure-durable-functions


    【解决方案1】:

    谢谢neilbmclaughlin。发布您的建议作为帮助其他社区成员的答案。

    生成器函数:

    module.exports = function* orchestratorFunctionGenerator(context) {
      const input = context.df.getInput();
      const apimApiName = input.apimApiName;
      const indexNames = yield context.df.callActivity('GetIndexNames', apimApiName);
      const indexerName = indexNames.idle;
      const indexerStatus = yield context.df.callActivity('GetIndexerStatus', indexerName);
      return indexerStatus;
    };
    
    

    基本测试:

    const chai = require('chai');
    const sinon = require('sinon');
    const getIndexerStatusOrchestratorGenerator = require('../../GetIndexerStatusOrchestrator/generator-function');
    
    const expect = chai.expect;
    
    function iterateGenerator(generator) {
      let result = generator.next();
      while (!result.done) {
        result = generator.next(result.value);
      }
      return result;
    }
    
    describe('getIndexerStatusOrchestrator', () => {
      it('happy path should return \'inProgress\'', () => {
        const indexNames = { active: 'index-1', idle: 'index-2' };
        const apimApiName = 'api';
        const input = { apimApiName };
        const stubCallActivity = sinon.stub();
        stubCallActivity.withArgs('GetIndexNames', apimApiName).returns(indexNames);
        stubCallActivity.withArgs('GetIndexerStatus', indexNames.idle).returns('inProgress');
    
        const context = {
          df: {
            callActivity: stubCallActivity,
            getInput: sinon.fake.returns(input),
          },
        };
    
        const generator = getIndexerStatusOrchestratorGenerator(context);
    
        const result = iterateGenerator(generator);
        expect(result.value).to.equal('inProgress');
      });
      it('indexer status should be for the idle index', () => {
        const indexNames = { active: 'index-1', idle: 'index-2' };
        const apimIndexName = 'api';
        const input = { apimApiName: apimIndexName };
        const stubCallActivity = sinon.stub();
        stubCallActivity.withArgs('GetIndexNames', apimIndexName).returns(indexNames);
        stubCallActivity.withArgs('GetIndexerStatus', indexNames.idle);
        // use stub as a mock since we need both stub and mock behaviour
        // for 'callActivity' and this was the easier option
        stubCallActivity.withArgs('GetIndexerStatus').callsFake((method, indexerName) => {
          expect.fail(`Unexpected indexer name ${indexerName}`);
        });
    
        const context = {
          df: {
            callActivity: stubCallActivity,
            getInput: sinon.fake.returns(input),
          },
        };
    
        const generator = getIndexerStatusOrchestratorGenerator(context);
    
        iterateGenerator(generator);
    
        // expectations set above
      });
    });
    

    您可以参考How to implement a basic unit test in javascript for an azure durable functiongenerator-function.jsDeploymentOrchestrator.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 2014-11-17
      • 2016-05-04
      • 1970-01-01
      • 2021-10-29
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多