在 NodeJs 中模拟外部 API 进行单元测试
Mocking external API for unit testing in NodeJs
我现在花了几个小时搜索用于在 nodejs 中模拟外部 api 的库。我已经尝试过 SinonJS 库,但它似乎不适用于外部网络调用...有人知道另一个允许模拟外部网络调用的库吗?或者有人有 SinonJS 的工作示例吗?
感谢您的每一个回答!
你可以在下面找到我的代码。
我使用 SinonJS 的单元测试:
import expect from 'expect';
import { searchForProducts } from 'api/ProductAPI';
import emptyResults from 'api/ProductAPI_EmptyResult.json';
describe('ProductAPI', () => {
let server;
before(function () {
server = sinon.fakeServer.create();
server.respondWith(
"GET",
"https://my.domain.com/myresource",
[200, { "Content-Type": "application/json" }, JSON.stringify(emptyResults)]
);
});
it('product search with working API ', () => {
server.respond();
searchForProducts('tv').then(
(data) => {
console.log('success');
},
(error) => {
console.log('error');
});
//dummy expect
expect(
'test'
).toEqual('test');
});
});
产品API:
import restClient from './RestClient';
/**
*
* Returns a list of products
* @param query
* @returns {ProductDTOs}
*/
function _searchForProducts(query) {
return restClient().get(
`/myresource`
);
}
RestClient(使用 Axios):
import Axios from 'axios';
const restClient = function restClient() {
let axios;
let apiUrl;
function _url(url) {
return `${apiUrl}${url}`;
}
function _get(url) {
return axios({
method: 'GET',
url: _url(url),
});
}
function _post(url, data) {
return axios({
method: 'POST',
url: _url(url), data,
});
}
function _put(url, data) {
return axios({
method: 'PUT',
url: _url(url), data,
});
}
function _patch(url, data) {
return axios({
method: 'PATCH',
url: _url(url),
data,
});
}
function _delete(url, data) {
return axios({
method: 'DELETE',
url: _url(url), data,
});
}
function _setDefaultHeaders() {
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.common['Accept'] = 'application/json';
}
function _setAuthorizationHeader(token) {
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
}
function _removeAuthorizationHeader() {
axios.defaults.headers.common['Authorization'] = '';
}
function _setup() {
axios = Axios;
apiUrl = 'https://my.domain.com';
_setDefaultHeaders();
}
_setup();
return {
setup: _setup,
url: _url,
get: _get,
post: _post,
put: _put,
patch: _patch,
delete: _delete,
setDefaultHeaders: _setDefaultHeaders,
setAuthorizationHeader: _setAuthorizationHeader,
removeAuthorizationHeader: _removeAuthorizationHeader,
};
};
export default restClient;
在 sinon 或任何其他测试套件(如 jasmine)中使用间谍功能应该是模拟外部调用的适当方式。
我发现了我的问题 我忘记了一些服务器配置。为了使服务器 运行 我必须将 autoRespond 属性设置为 true:
before(function () {
server = sinon.fakeServer.create();
server.respondWith(
"GET",
"https://ec2-52-58-48-214.eu-central-1.compute.amazonaws.com/mspocc/v2/msp/products/search?query=tv&fields=FULL",
[200, { "Content-Type": "application/json" }, JSON.stringify(emptyResults)]
);
server.autoRespond = true
});
我现在花了几个小时搜索用于在 nodejs 中模拟外部 api 的库。我已经尝试过 SinonJS 库,但它似乎不适用于外部网络调用...有人知道另一个允许模拟外部网络调用的库吗?或者有人有 SinonJS 的工作示例吗?
感谢您的每一个回答!
你可以在下面找到我的代码。
我使用 SinonJS 的单元测试:
import expect from 'expect';
import { searchForProducts } from 'api/ProductAPI';
import emptyResults from 'api/ProductAPI_EmptyResult.json';
describe('ProductAPI', () => {
let server;
before(function () {
server = sinon.fakeServer.create();
server.respondWith(
"GET",
"https://my.domain.com/myresource",
[200, { "Content-Type": "application/json" }, JSON.stringify(emptyResults)]
);
});
it('product search with working API ', () => {
server.respond();
searchForProducts('tv').then(
(data) => {
console.log('success');
},
(error) => {
console.log('error');
});
//dummy expect
expect(
'test'
).toEqual('test');
});
});
产品API:
import restClient from './RestClient';
/**
*
* Returns a list of products
* @param query
* @returns {ProductDTOs}
*/
function _searchForProducts(query) {
return restClient().get(
`/myresource`
);
}
RestClient(使用 Axios):
import Axios from 'axios';
const restClient = function restClient() {
let axios;
let apiUrl;
function _url(url) {
return `${apiUrl}${url}`;
}
function _get(url) {
return axios({
method: 'GET',
url: _url(url),
});
}
function _post(url, data) {
return axios({
method: 'POST',
url: _url(url), data,
});
}
function _put(url, data) {
return axios({
method: 'PUT',
url: _url(url), data,
});
}
function _patch(url, data) {
return axios({
method: 'PATCH',
url: _url(url),
data,
});
}
function _delete(url, data) {
return axios({
method: 'DELETE',
url: _url(url), data,
});
}
function _setDefaultHeaders() {
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.common['Accept'] = 'application/json';
}
function _setAuthorizationHeader(token) {
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
}
function _removeAuthorizationHeader() {
axios.defaults.headers.common['Authorization'] = '';
}
function _setup() {
axios = Axios;
apiUrl = 'https://my.domain.com';
_setDefaultHeaders();
}
_setup();
return {
setup: _setup,
url: _url,
get: _get,
post: _post,
put: _put,
patch: _patch,
delete: _delete,
setDefaultHeaders: _setDefaultHeaders,
setAuthorizationHeader: _setAuthorizationHeader,
removeAuthorizationHeader: _removeAuthorizationHeader,
};
};
export default restClient;
在 sinon 或任何其他测试套件(如 jasmine)中使用间谍功能应该是模拟外部调用的适当方式。
我发现了我的问题 我忘记了一些服务器配置。为了使服务器 运行 我必须将 autoRespond 属性设置为 true:
before(function () {
server = sinon.fakeServer.create();
server.respondWith(
"GET",
"https://ec2-52-58-48-214.eu-central-1.compute.amazonaws.com/mspocc/v2/msp/products/search?query=tv&fields=FULL",
[200, { "Content-Type": "application/json" }, JSON.stringify(emptyResults)]
);
server.autoRespond = true
});