Node.js 在模拟AWS SDK时,如何使用节点模拟http?
我有一个现有的工作测试,正在测试Express应用程序中的路线(修剪代码): 这是一个有效的测试。但我需要重构它,使之不使用完整的服务器(因为它在启动时正在进行一些集成)。所以我引入了Node.js 在模拟AWS SDK时,如何使用节点模拟http?,node.js,unit-testing,amazon-web-services,aws-sdk-mock,node-mocks-http,Node.js,Unit Testing,Amazon Web Services,Aws Sdk Mock,Node Mocks Http,我有一个现有的工作测试,正在测试Express应用程序中的路线(修剪代码): 这是一个有效的测试。但我需要重构它,使之不使用完整的服务器(因为它在启动时正在进行一些集成)。所以我引入了节点模拟http: const httpMocks = require('node-mocks-http'); const feedbackRouteHandler = require('./feedback'); ... before(() => { sendEmailMock = sinon
节点模拟http
:
const httpMocks = require('node-mocks-http');
const feedbackRouteHandler = require('./feedback');
...
before(() => {
sendEmailMock = sinon.stub().callsArgWith(1, null, 'All is well');
AWS.mock('SES', 'sendEmail', sendEmailMock);
});
...
const mockRequest = httpMocks.createRequest({
method: 'POST',
url: '/gethelp/api/v1/feedback',
body: {
thumbsUp: 'true',
title: 'Abcdef ghi',
url: 'http://google.com',
comments: 'lkajsdj lkajsdkjf aslkjdfa asjdflasjd lkfj',
},
});
const mockResponse = httpMocks.createResponse();
feedbackRouteHandler(mockRequest, mockResponse);
expect(mockResponse.statusCode).to.equal(200);
expect(sendEmailMock.args).to.exist;
问题在于,添加节点mocks http
似乎破坏了对AWS SDK的模拟。当点击sendmail
时,它点击的是实际的AWS SDK,而不是模拟版本。它在测试的前一个版本中击中了模拟版本
如何将节点模拟http
与aws sdk模拟一起使用
const httpMocks = require('node-mocks-http');
const feedbackRouteHandler = require('./feedback');
...
before(() => {
sendEmailMock = sinon.stub().callsArgWith(1, null, 'All is well');
AWS.mock('SES', 'sendEmail', sendEmailMock);
});
...
const mockRequest = httpMocks.createRequest({
method: 'POST',
url: '/gethelp/api/v1/feedback',
body: {
thumbsUp: 'true',
title: 'Abcdef ghi',
url: 'http://google.com',
comments: 'lkajsdj lkajsdkjf aslkjdfa asjdflasjd lkfj',
},
});
const mockResponse = httpMocks.createResponse();
feedbackRouteHandler(mockRequest, mockResponse);
expect(mockResponse.statusCode).to.equal(200);
expect(sendEmailMock.args).to.exist;