![](/img/trans.png)
[英]how to get the return value of an async function after then is called on the async function
[英]How to mock return value of async function?
我正在嘗試測試使用“ easy-soap-request”庫的功能。 我想模擬“ soapRequest”函數返回的結果。
我已經嘗試過了,但是沒有用,我一直從外部API獲取數據。
client.js
const soapRequest = require('easy-soap-request');
async function get_data(){
var response = await soapRequest(url, auth_headers) //this is what I want to mock
var result;
result = some_parsing_function(response); //this is what I want test
return result;
}
test.js
const client = require('../../client');
describe('get_data tests', () =>{
it('should test sth', function (done) {
var stubed = stub(client, 'soapRequest').returns('dummy value');
client.get_data().then((result) => {
//assertions
console.log(result) //result still has value from external service
done();
});
})
});
編輯:
因此,我嘗試按照答案之一的建議使用sinon.fake()。
const client = require('../../client');
describe('get_data tests', () =>{
it('should test sth', function (done) {
var fake_soap = fake(async () => {
return 12345;
});
replace(cilent, 'soapRequest', fake_soap);
client.soapRequest().then((res) => {
console.log(res); // 12345
})
client.get_data().then((result) => {
//assertions
console.log(result) //still original value from external service
done();
});
})
});
在源文件中, soapRequest
變量本身是一個未命名為import(對象)的函數,因此僅依靠sinon.stub
是不可能的。
如果看一下easy-soap-request
源代碼,很明顯,它會導出一個函數https://github.com/circa10a/easy-soap-request/blob/master/index.js#L14
根據我的經驗,對於這種情況,可以通過添加如下所示的proxyquire
來解決。
const proxyquire = require('proxyquire');
const sinon = require('sinon');
// we mock `easy-soap-request` (the library name) by using `sinon.stub`
const client = proxyquire('../../client', { 'easy-soap-request': sinon.stub().returns('dummy value') })
describe('get_data tests', () =>{
it('should test sth', async () => { // use async/await for better code
const result = await client.get_data();
console.log(result); // dummy value
})
});
如果您不想使用sinon
,也可以這樣做
const client = proxyquire('../../client', { 'easy-soap-request': () => 'dummy value' })
參考:
https://www.npmjs.com/package/proxyquire
希望能幫助到你
這樣可以:
let asyncHandler = sinon.fake(async () => {});
更新:
我的錯! 我對這個問題不太了解。 這是解決它的一種方法。 此外,最好將外部依賴項與應用程序邏輯分開。 因此,我添加了一個新的文件來請求肥皂。 然后,取消測試請求變得容易。
soap-connector.js
const soapRequest = require('easy-soap-request');
exports.request = async function request(url, auth_headers) {
return soapRequest(url, auth_headers);
};
client.js
const soapConnector = require('./soap-connector');
async function get_data(){
var response = await soapConnector.request(url, auth_headers) //this is what I want to mock
var result;
result = some_parsing_function(response); //this is what I want test
return result;
}
test.js
const soapConnector = require('../../soap-connector');
const client = require('../../client');
describe('get_data tests', () =>{
it('should test sth', function (done) {
var stubed = stub(soapConnector, 'request').callsFake(async () => {
return 12345;
});
client.get_data().then((result) => {
//assertions
console.log(result) //result still has value from external service
done();
});
})
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.