Javascript 用2个参数模拟下一个函数?
我正在尝试模拟https。get与嵌套在我的自定义函数Javascript 用2个参数模拟下一个函数?,javascript,node.js,ecmascript-6,jestjs,Javascript,Node.js,Ecmascript 6,Jestjs,我正在尝试模拟https。get与嵌套在我的自定义函数getSecureString()中https.get返回在其try/catch部分使用的两个json元素。请参阅下面的函数 网上版本 我得到的错误是 Expected: 1 Received: undefined 25 | 26 | it('should return 1', () => { > 27 | expect(result).toEqual(1) |
getSecureString()
中https.get
返回在其try/catch部分使用的两个json元素。请参阅下面的函数
网上版本
我得到的错误是
Expected: 1
Received: undefined
25 |
26 | it('should return 1', () => {
> 27 | expect(result).toEqual(1)
| ^
28 | })
29 | });
30 |
at Object.<anonymous> (getSecureString.test.js:27:22)
getSecureString.test.js
const fs = require('fs');
const https = require('https');
var uuid = require('uuid');
const {v4: uuidv4} = require('uuid');
module.exports = async function getSecureString(options) {
options = options || {};
options.hostname = options.hostname || {};
options.path = options.path || '/';
options.string = options.string || {};
options.statusCode = options.statusCode || {};
options.aftaleId = options.aftaleId || {};
options.certFile = options.certFile || {};
options.keyFile = options.keyFile || {};
options.timeout = options.timeout || 0;
const opt = {
hostname: options.hostname,
port: 443,
path: options.path,
method: 'GET',
cert: fs.readFileSync(options.certFile),
key: fs.readFileSync(options.keyFile),
headers: {'AID': options.aftaleId,
'TID': uuidv4(),
'RID': uuidv4()
},
};
opt.agent = new https.Agent(opt);
try {
const r = await httpsGet(opt, options.timeout);
return (r.statusCode === options.statusCode && r.body.includes(options.string)) ? 1 : 0;
} catch (error) {
console.error(error);
}
};
function httpsGet(opts, timeout) {
return new Promise((resolve, reject) => {
const req = https.get(opts, (res) => {
let body = '';
res.on('data', (data) => {
body += data.toString();
});
res.on('end', () => {
resolve({body, statusCode: res.statusCode});
});
});
req.setTimeout(timeout, function() {
req.destroy('error');
});
req.on('error', (e) => {
console.error(e);
reject(e);
});
});
};
const getSecureString = require('./getSecureString');
const https = require('https');
jest.mock('https');
describe("getSecureString ", () => {
describe('when httpsGet returns expected statusCode and body includes expected string', () => {
let result;
beforeAll(async () => {
https.get.mockResolvedValue({
body: 'xyz secret_string xyz',
//res.statusCode: 200
})
result = await getSecureString({
hostname: 'encrypted.google.com',
path: '/',
string: 'secret_string',
statusCode: 200,
aftaleId: 1234,
certFile: './test.crt',
keyFile: './test.key',
timeout: 1000,
})
});
it('should return 1', () => {
expect(result).toEqual(1)
})
});
describe('when httpsGet returns expected statusCode and body includes expected string', () => {
let result;
beforeAll(async () => {
https.get.mockResolvedValue({
body: 'xyz secret_string xyz',
//res.statusCode: 200
})
result = await getSecureString({
hostname: 'encrypted.google.com',
path: '/',
string: 'not_secret_string',
statusCode: 201,
aftaleId: 1234,
certFile: './test.crt',
keyFile: './test.key',
timeout: 1000,
})
});
it('should return 0', () => {
expect(result).toEqual(0)
})
});
describe("when an exception is thrown", () => {
let result;
beforeAll(async () => {
// mockRejected value returns rejected promise
// which will be handled by the try/catch
https.get.mockRejectedValue({
body: 'xyz secret_string xyz',
//res.statusCode: 200
})
result = await getSecureString();
})
it('should return -1', () => {
expect(result).toEqual(-1)
})
});
});
问题:
有人能找出我为什么会出现上述错误,以及我如何在
https.get()
中模拟body
和res.statusCode
?这里有一个例子,但如果有什么不清楚的地方,我可能明天会描述它们:)哇!谢谢我有一个问题=)我现在在添加了httpsGet()
的输出作为文件。我想,如果我把它包括进去,或者仅仅是字符串:“not_secret\u string”
,但看到您使用了3dataCallback()
s,我开始认为使用httpsGet.mockResponseData.json
中的内容不会那么容易?顺便问一下,你是如何触发聊天链接的?我已经创建了一个聊天室并将链接粘贴到了评论中。否则,SO会提示您在第7条或第8条评论之后移动聊天中的对话OK,这里的评论中是否应该有聊天链接?