如何在Cucumber测试中测试promise的返回值?

问题描述 投票:1回答:1

我正在尝试测试getdb请求到couchdb节点的返回值。

我有一个使用以下Given子句定义的功能:

Given A get request is made to the DB

使用以下步骤函数实现:

var Profile = require('../UserProfile.js')
Given('A get request is made to the DB', function () {

    var text = Profile.getDB('localhost:5984').then(data => {
        console.log(data)
    })

}); 

上面的步骤引用了这个模型:

var axios = require('axios')

module.exports = {

    getDB: function(url){               
        return axios.get(url).then(response => {
            return response.data
        })

    }
};

当我在模型中执行它并在步骤定义中引用它时,我似乎无法记录GET请求的结果。当我在步骤定义中执行GET请求时,它可以工作 - 但这对我没用,我想测试模型。如何获得结果值?

asynchronous cucumber couchdb axios cucumberjs
1个回答
3
投票

Cucumber 2.0 supports Promises as returns,尝试:

const Profile = require('../UserProfile')
const { defineSupportCode } = require('cucumber')

defineSupportCode(({ defineStep }) => {
   defineStep('A get request is made to the DB', function () {
      return Profile.getDB('http://localhost:5984').then(data => {
        console.log(data)
      })
   })
})
© www.soinside.com 2019 - 2024. All rights reserved.