在 Mocha 中开始所有测试之前发出请求
Making a request before all tests start in Mocha
我想测试我的简单 API 有 /groups
URL。
我想在所有测试开始之前向那个 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:预期 {} 有 属性 'length'
总结: 如何将 response
从 axios inside 传递到 in()
?
您的第一个表单不正确,因为您没有返回链式承诺。因此,mocha 无法知道您的 before
何时完成,甚至根本不知道它是异步的。您的第二种形式将解决这个问题,但是由于 axios.get
已经 returns 一个承诺,所以不使用 mocha 的内置承诺支持有点浪费。
至于使响应在 it
中可见,您需要将其分配给范围内的变量,该范围将在 it
.
中可见
var expect = require('chai').expect
var axios = require('axios')
var response;
describe('Categories', function() {
describe('Groups', function() {
before(function() {
// Note that I'm returning the chained promise here, as discussed.
return axios.get(config.hostname + '/groups').then(function (res) {
// Here's the assignment you need.
response = res;
})
});
// This test does not need the `done` because it is not asynchronous.
// It will not run until the promise returned in `before` resolves.
it('returns a not empty set of results', function() {
expect(response).to.have.length.greaterThan(0);
})
});
});
我想测试我的简单 API 有 /groups
URL。
我想在所有测试开始之前向那个 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:预期 {} 有 属性 'length'
总结: 如何将 response
从 axios inside 传递到 in()
?
您的第一个表单不正确,因为您没有返回链式承诺。因此,mocha 无法知道您的 before
何时完成,甚至根本不知道它是异步的。您的第二种形式将解决这个问题,但是由于 axios.get
已经 returns 一个承诺,所以不使用 mocha 的内置承诺支持有点浪费。
至于使响应在 it
中可见,您需要将其分配给范围内的变量,该范围将在 it
.
var expect = require('chai').expect
var axios = require('axios')
var response;
describe('Categories', function() {
describe('Groups', function() {
before(function() {
// Note that I'm returning the chained promise here, as discussed.
return axios.get(config.hostname + '/groups').then(function (res) {
// Here's the assignment you need.
response = res;
})
});
// This test does not need the `done` because it is not asynchronous.
// It will not run until the promise returned in `before` resolves.
it('returns a not empty set of results', function() {
expect(response).to.have.length.greaterThan(0);
})
});
});