【问题标题】:sinon stub does not recognise a call to a stubsinon 存根无法识别对存根的调用
【发布时间】:2019-03-25 21:36:45
【问题描述】:

这是要测试的代码:

    const AWS = require('aws-sdk');
const { APPLICATIONS, NOTIFICATION_FREQUENCIES } = require('./config');

exports.createHandler = ({ notificationService }) => async (event, context) => {

  try{
    Object.values(APPLICATIONS).forEach(async appId => {

      const notifications =  await notificationService
        .getNotificationsByApplication(appId);



      const dailyNotifications =notifications.filter(
        e =>
          e.frequency === NOTIFICATION_FREQUENCIES.DAILY,
      );

      console.log('dailyNo', dailyNotifications);

      const dailyTemplate = notificationService.prepareDailyTemplate(
        dailyNotifications
      );
      console.log('dailyTemplate', dailyTemplate);

      notificationService.notifyToAdmin(dailyTemplate);
    });
  }
  catch(err) {
    console.log(err);
  }

};

这是我使用 sinon 的测试:

const sinon = require('sinon');
const { APPLICATIONS, NOTIFICATION_FREQUENCIES } = require('../lib/config');
describe('Daily notifier tests', () => {

  it('should prepare daily template for each of the applications', () => {

    const notificationService = require('../lib/notificationService').createHandler({
      commands: {},
      simpleMailService: {},
    });

    const notifications = [
      {
        type: 'create_order',
        frequency: NOTIFICATION_FREQUENCIES.DAILY,
      },
      {
        type: 'create_order',
        frequency: NOTIFICATION_FREQUENCIES.DAILY,
      },
      {
        type: 'create_order',
        frequency: NOTIFICATION_FREQUENCIES.MONTHLY,
      },
    ];
    const template = 'some html template as string';

    sinon.stub(notificationService, 'getNotificationsByApplication').resolves(notifications);
    sinon.stub(notificationService, 'prepareDailyTemplate').returns(template);
    sinon.stub(notificationService, 'notifyToAdmin');

    const sut = require('../lib/dailyNotifier').createHandler({
      notificationService,
    });

    const event = {};
    const context = {};


    sut(event, context);

    const dailyNotifications = [
      {
        type: 'create_order',
        frequency: NOTIFICATION_FREQUENCIES.DAILY,
      },
      {
        type: 'create_order',
        frequency: NOTIFICATION_FREQUENCIES.DAILY,
      }
    ];

    sinon.assert.calledOnce(notificationService.prepareDailyTemplate);
    sinon.assert.calledWith(notificationService.notifyToAdmin, template);


  });

});

根据sinon的说法,prepareDailyTemplate方法根本没有被调用(0次),但是当我执行测试时,我什至可以看到console.log'dailyTemplate',这意味着该方法已经执行了一次。 错误信息:

AssertError: expected prepareDailyTemplate to be called once but was called 0 times

我做错了什么?

【问题讨论】:

    标签: javascript unit-testing sinon


    【解决方案1】:

    sut 是由createHandler 创建的async 函数,因此它返回Promise

    你只需要await它返回的Promise

    it('should prepare daily template for each of the applications', async () => {  // <= async
    
      // ...
    
      await sut(event, context);  // <= await
    
      // ...
    
      sinon.assert.calledOnce(notificationService.prepareDailyTemplate);  // Success!
    });
    

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 2015-11-22
      • 2015-11-28
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      相关资源
      最近更新 更多