aws-sdk-mock验证服务ctor调用SNS

时间:2019-02-26 20:31:31

标签: javascript jestjs aws-sdk-mock

我正在尝试验证是否已为SNS的服务ctor提供了正确的参数,但我不知道该怎么做。

现在,我确实知道如何验证发布,但是,我再次尝试验证对SNS功能/ ctor的期望。

这是一些伪代码:

//code
const AWS = require('aws-sdk');
const SNS = new AWS.SNS({bobby:'no'});

//test
const AWSmock = require('aws-sdk-mock');

describe('something', () => {
    beforeAll(()=>{
        AWSmock.mock('SNS','publish', Promise.resolve());
    });
    test('doing it', () => {
        const f = require('file');

        expect(AWSmock.SNS.calledWith({})).toEqual(true); //this example would be false, but I can't figure out how to reference the SNS method here
    });
});

1 个答案:

答案 0 :(得分:0)

摘自aws-sdk-mock的文档:

在顶层node_modules项目文件夹中不包含`aws-sdk`的项目结构将不会被正确模拟。一个例子是在嵌套项目目录中安装“ aws-sdk”。您可以通过使用setSDK()将显示的路径显式设置为嵌套的aws-sdk模块来解决。
//code
const AWS = require('aws-sdk');
const SNS = new AWS.SNS({bobby:'no'});

//test
const AWSmock = require('aws-sdk-mock');
// setting the AWS explicitly might help
AWSMock.setSDKInstance(AWS);

describe('something', () => {
    beforeAll(()=>{
        AWSmock.mock('SNS','publish', Promise.resolve());
    });
    test('doing it', () => {
        const f = require('file');

        expect(AWSmock.SNS.calledWith({})).toEqual(true); //this example would be false, but I can't figure out how to reference the SNS method here
    });
});