简体   繁体   English

如何模拟异步函数的返回值?

[英]How to mock return value of async function?

I'm trying to test function which uses 'easy-soap-request' library. 我正在尝试测试使用“ easy-soap-request”库的功能。 I want to mock results returned by 'soapRequest' function. 我想模拟“ soapRequest”函数返回的结果。

I've tried this but it didn't worked, I keep getting data from external API. 我已经尝试过了,但是没有用,我一直从外部API获取数据。

client.js 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 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();
        });

    })
});

EDIT: 编辑:

So I've tried using sinon.fake() as suggested by one of the answers. 因此,我尝试按照答案之一的建议使用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();
        });

    })
});

In the source file, soapRequest variable itself is a function not a named import (object) so it is impossible to rely on just sinon.stub . 在源文件中, soapRequest变量本身是一个未命名为import(对象)的函数,因此仅依靠sinon.stub是不可能的。

If take a look at easy-soap-request source code, obviously, it exports a function https://github.com/circa10a/easy-soap-request/blob/master/index.js#L14 如果看一下easy-soap-request源代码,很明显,它会导出一个函数https://github.com/circa10a/easy-soap-request/blob/master/index.js#L14

Based on my experience, for this case, it can be solved by adding proxyquire like below. 根据我的经验,对于这种情况,可以通过添加如下所示的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
    })
});

If you don't want to use sinon , you can also do this 如果您不想使用sinon ,也可以这样做

const client = proxyquire('../../client', { 'easy-soap-request': () => 'dummy value' })

Reference: 参考:

https://www.npmjs.com/package/proxyquire https://www.npmjs.com/package/proxyquire

Hope it helps 希望能帮助到你

This will do: 这样可以:

 
 
 
  
  let asyncHandler = sinon.fake(async () => {});
 
  

Documentation 文献资料

Update: 更新:

My bad! 我的错! I did not understood the problem well. 我对这个问题不太了解。 Here is one way to solve it. 这是解决它的一种方法。 More over, it is better to keep external dependencies separate from the application logic. 此外,最好将外部依赖项与应用程序逻辑分开。 So, I added a new file for making soap request. 因此,我添加了一个新的文件来请求肥皂。 Then, stubbing the request for testing becomes easy. 然后,取消测试请求变得容易。

soap-connector.js soap-connector.js

const soapRequest = require('easy-soap-request');
exports.request = async function request(url, auth_headers) {
  return soapRequest(url, auth_headers);
};

client.js 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 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();
        });

    })
});

Documentation 文献资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM