我有以下课程:
class QueueRequest{
constructor(){
this.queue = [];
}
async getKeepAliveRequestLibrary(httpOptions){
//creation of special type of request with Keep-Alive
const httpModule = (airConf.useSecureConnection ? https : http);
const keepAliveRequest = request.defaults({
agent: new httpModule.Agent({
keepAlive: true,
maxSockets: airConf.maxSockets,
})
});
let response = await keepAliveRequest(httpOptions);
return response;
}
async addRequestToQueue(httpOptions){
this.queue.push(httpOptions);
let response = await this.getKeepAliveRequestLibrary(this.queue[0]);
this.queue.shift();
return response;
}
}
module.exports = QueueRequest;
我尝试通过以下方式测试课程:
const sinon = require('sinon');
const rp = require('request-promise-native');
//override the original common, so we can manipulate the config
jest.mock('./common');
//local dependencies
require('./common').prepareConfigForTaskHandler(13);
const QueueRequest = require('./queue-request');
let queue = new QueueRequest();
describe('Testing QueueRequest module:', () => {
//setup
it('checking proper queueing one request', () => {
sinon.stub(rp, 'Request').resolves({});
//test
});
//teardown
it('checking proper queueing many requests', () => {
});
});
我的问题是将模块作为一个整体进行测试。我的意图是在没有提出真实要求的情况下进行测试。我想进行两个测试,一个测试仅用于一个测试,第二个用于多个测试。
答案 0 :(得分:0)
您可以使用Nock(https://github.com/nock/nock)模拟请求。您可以指定url,标题和响应期望。 例如: 我想模拟对服务的GET请求,并且希望使用特定的有效负载作为statusCode 200的响应。
nock(baseUrl, {reqheaders: {"Authorization": "Bearer <token>"}}.get(path).reply(200, {result:{ "Id": 1}})