【问题标题】:Stubing AWS SQS client存根 AWS SQS 客户端
【发布时间】:2021-09-02 03:13:13
【问题描述】:

我正在尝试存根 aws-sdk/client-sqs 调用。我对 aws 和存根都是新手,我在文档中找不到太多与使用 mocha/chai 存根有关的内容

我的文件

import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand } from '@aws-sdk/client-sqs'
const sqsClient = new SQSClient()

const params = {
    AttributeNames: ['SentTimestamp'],
    MaxNumberOfMessages: 10,
    QueueUrl: paymentsUrl,
    //....more params
}

const getPolledMessages = async sqsClient => {

    const data = await sqsClient.send(new ReceiveMessageCommand(params))
    //....continue to do stuff with the data
}

我的测试如下。我从这个test 大量借用了@aws-sdk/client-ses 的存根

import { ReceiveMessageCommand, SQSClient } from '@aws-sdk/client-sqs'
import { expect } from 'chai'


describe('mock sqs', () => {
  let stub;
  before(() => {
    stub = sinon.stub(SQSClient.prototype, 'ReceiveMessageCommand')
  })
  after(() => stub.restore())

  it('can send', async () => {
    await SQSClient.send(new ReceiveMessageCommand(params))
    expect(stub.calledOnce).to.be.true
  })
})

目前出现以下错误

 TypeError: Cannot stub non-existent property ReceiveMessageCommand
 at Function.stub (node_modules/sinon/lib/sinon/stub.js:73:15)
 at Sandbox.stub (node_modules/sinon/lib/sinon/sandbox.js:333:37)

【问题讨论】:

    标签: javascript mocha.js chai sinon aws-sdk-js


    【解决方案1】:

    您可以stub out dependencies with link seams.。我们应该使用proxyquire 包来做到这一点。

    例如

    index.ts:

    import { SQSClient, ReceiveMessageCommand } from '@aws-sdk/client-sqs';
    const sqsClient = new SQSClient({ region: 'REGION' });
    
    const params = {
      AttributeNames: ['SentTimestamp'],
      MaxNumberOfMessages: 10,
      QueueUrl: 'paymentsUrl',
    };
    
    export const getPolledMessages = async () => {
      const data = await sqsClient.send(new ReceiveMessageCommand(params));
    };
    

    index.test.ts:

    import proxyquire from 'proxyquire';
    import sinon from 'sinon';
    
    describe('68017252', () => {
      it('should pass', async () => {
        const sqsClientInstance = {
          send: sinon.stub(),
        };
        const SQSClient = sinon.stub().returns(sqsClientInstance);
        const ReceiveMessageCommand = sinon.stub();
        const { getPolledMessages } = proxyquire('./', {
          '@aws-sdk/client-sqs': {
            SQSClient,
            ReceiveMessageCommand,
          },
        });
        await getPolledMessages();
        sinon.assert.calledWithExactly(SQSClient, { region: 'REGION' });
        sinon.assert.calledOnce(sqsClientInstance.send);
        sinon.assert.calledWithExactly(ReceiveMessageCommand, {
          AttributeNames: ['SentTimestamp'],
          MaxNumberOfMessages: 10,
          QueueUrl: 'paymentsUrl',
        });
      });
    });
    

    测试结果:

      68017252
        ✓ should pass (3331ms)
    
    
      1 passing (3s)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |     100 |      100 |     100 |     100 |                   
     index.ts |     100 |      100 |     100 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多