【问题标题】:Sinon cannot verify if internal function is called onceSinon 无法验证内部函数是否被调用过一次
【发布时间】:2020-09-09 20:09:40
【问题描述】:

给定两个函数:

export const func1 = (accessKeyId, secretAccessKeyID, region, queue, body) =>
  new AWS.SQS({ accessKeyId, secretAccessKey: secretAccessKeyID, region })
    .sendMessage({ QueueUrl: queue, MessageBody: body })
    .promise();

export const func2 = (queue, body) => func1(config.key, config.secret, config.region, queue, body);

直截了当的功能。我想如何测试 func2,并验证我是否使用正确的参数调用了一次 func1,所以我有:

import { func1, func2 } from '../uploads';
import sinon from 'sinon';

describ('test 1', ()=> {
it('send job queue should send correct param to sendToSqs', async done => {
    const spy = sinon.spy(func2);
    const queue = 'test-queue';
    const topic = 'test-topic';
    const data = { field1: 'value1', field2: 'value2' };

    await func1(queue, topic, data);
    expect(spy.calledOnce).to.be.equal(true);
    done();
  });
});

上面运行会报错:

(node:29076) UnhandledPromiseRejectionWarning: UnknownEndpoint: Inaccessible host: `test-queue'. This service may not be available in the `us-east-1' region.

显然,即使创建了 spy,它仍然会尝试调用其中的函数。所以我认为我在这里有两个问题: 1.spy 似乎与func2 没有关联,我错过了什么? 2. 我也应该嘲笑func1。但 mock 仅适用于对象内部的函数。如何模拟没有父对象的函数?

【问题讨论】:

    标签: javascript testing mocha.js chai sinon


    【解决方案1】:

    您不能对独立函数进行存根/监视。而且,在func2 内部调用的func1 将始终是原始func1,而不是存根版本。您需要进行一些更改:

    例如

    index.ts:

    import AWS from 'aws-sdk';
    
    const config = {
      key: 'key',
      secret: 'secret',
      region: 'region',
    };
    
    export const func1 = (accessKeyId, secretAccessKeyID, region, queue, body) =>
      new AWS.SQS({ accessKeyId, secretAccessKey: secretAccessKeyID, region })
        .sendMessage({ QueueUrl: queue, MessageBody: body })
        .promise();
    
    export const func2 = (queue, body) => exports.func1(config.key, config.secret, config.region, queue, body);
    

    index.test.ts:

    import * as funcs from './';
    import sinon from 'sinon';
    
    describe('61964269', () => {
      it('should pass', async () => {
        const spy = sinon.stub(funcs, 'func1');
        const queue = 'test-queue';
        const topic = 'test-topic';
        const data = { field1: 'value1', field2: 'value2' };
    
        await funcs.func2(queue, data);
        sinon.assert.calledWithExactly(spy, 'key', 'secret', 'region', 'test-queue', {
          field1: 'value1',
          field2: 'value2',
        });
      });
    });
    

    我们使用exports.func1import * as funcs from './',这样func1 将保存在一个对象中——module.exports 对象。然后,我们可以在我们的测试文件中存根func1,它会在我们调用func2时被调用。

    单元测试的结果:

      61964269
        ✓ should pass
    
    
      1 passing (11ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |   83.33 |      100 |      50 |      80 |                   
     index.ts |   83.33 |      100 |      50 |      80 | 10                
    ----------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 2019-10-17
      • 1970-01-01
      • 2011-12-25
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多