【发布时间】:2018-11-08 05:11:30
【问题描述】:
我想测试具有/groups URL 的简单API。
我想在所有测试开始之前向该 URL 发出 API 请求(使用 Axios),并使响应对所有测试函数可见。
我正在尝试使 response 可见但无法使其工作。我关注了一个类似的案例with filling out the DB upfront,但我的案例没有运气。
下面是我的简单测试文件:
var expect = require('chai').expect
var axios = require('axios')
var response = {};
describe('Categories', function() {
describe('Groups', function() {
before(function() {
axios.get(config.hostname + '/groups').then(function (response) {
return response;
})
});
it('returns a not empty set of results', function(done) {
expect(response).to.have.length.greaterThan(0);
done();
})
});
});
我还尝试了对before 函数的轻微修改:
before(function(done) {
axios.get(config.hostname + '/groups')
.then(function (response) {
return response;
}).then(function() {
done();
})
});
但也没有运气。
我得到的错误只是response 没有改变,也没有在it 中可见。 AssertionError:期望 {} 具有属性“长度”
总结:如何将response从axios内部传递给in()?
【问题讨论】:
标签: node.js api asynchronous mocha.js axios