小编Paw*_*ł W的帖子

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

我正在尝试测试使用“easy-soap-request”库的函数。我想模拟“soapRequest”函数返回的结果。

我试过这个,但没有用,我一直从外部 API 获取数据。

客户端.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;
}
Run Code Online (Sandbox Code Playgroud)

测试.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();
        });

    })
});
Run Code Online (Sandbox Code Playgroud)

编辑:

所以我尝试按照其中一个答案的建议使用 …

javascript unit-testing node.js sinon ecmascript-6

5
推荐指数
1
解决办法
1979
查看次数

从Loopback 3迁移到Loopback 4

有什么自动方法可以将Loopback 3中的项目迁移到Loopback 4?例如,我在JSON中有模型定义:

{"name": "Item",
  "base": "PersistedModel",
  "forceId": true,
  "properties": {
    "id": {
      "type": "number",
      "id": true
    },
    "created": {
      "type": "Date",
      "postgresql": {
        "dbDefault": "now()"
      }
    },
    "name": {
      "type": "string",
      "required": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以在LB4中自动创建相同的模型?

loopbackjs

4
推荐指数
1
解决办法
791
查看次数