如何在 Cucumber 测试中测试承诺的 return 值?
How to test the return value of a promise in a Cucumber test?
我正在尝试测试对 couchdb 节点的获取请求的 return 值。
我有一个使用以下 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 请求的结果。当我在步骤定义中执行 GET 请求时,它有效 - 但这对我没有用,我想测试模型。我如何获得结果值?
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)
})
})
})
我正在尝试测试对 couchdb 节点的获取请求的 return 值。
我有一个使用以下 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 请求的结果。当我在步骤定义中执行 GET 请求时,它有效 - 但这对我没有用,我想测试模型。我如何获得结果值?
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)
})
})
})