我已经为我的图书馆创建了验收测试。发布库时,它将使用尚无法访问的外部API服务。所以我创建了一个模拟json api服务器,它返回所需的JSON进行测试。要让测试通过,我需要做的是在发出请求的文件中手动更改API网址。
我想知道是否有办法在运行验收测试时使用模拟API网址,并在不运行验收测试时恢复到实时URL。以下是带有实时网址的代码片段。
return fetch('http://liveURL.com/api')
.then(response => {
if (response.status === 200) {
return response.json()
.then(myResponse => {
var theResponse = myResponse.id;
return theResponse;
});
} else {
return response.json().then(error => {
throw new Error(error.error);
});
}
});
仅在运行验收测试时我想要做的是更改网址,“http://liveURL.com/api”,我将请求发送到“http://localhost:3000/api”。
我正在使用的模拟服务器可以在这里找到:https://github.com/typicode/json-server
编辑:回答Bens的问题,这是我的package.json,我试图设置NODE_ENV
{
"name": "my-lib",
"version": "0.1.0",
"description": "",
"main": "lib/myLib",
"private": true,
"scripts": {
"lint": "gulp lint",
"pretest": "npm run lint",
"test": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter spec \"test/unit/**/*.spec.js\"",
"acceptance-test": "NODE_ENV=test cucumberjs",
"build": "gulp build"
},
"author": "Me",
"license": "ISC",
"devDependencies": {
"babel-preset-es2015": "^6.16.0",
"babel-register": "^6.16.3",
"babelify": "^7.3.0",
"browserify": "^13.1.0",
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"config-browserify": "^1.0.5",
"cors": "^2.8.1",
"cucumber": "^0.10.3",
"eslint": "^3.0.1",
"eslint-teamcity": "^1.1.0",
"express": "^4.14.0",
"gulp": "^3.9.1",
"gulp-eslint": "^3.0.1",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^2.0.0",
"isomorphic-fetch": "^2.2.1",
"istanbul": "v1.1.0-alpha.1",
"jsdom": "^9.8.3",
"json-server": "^0.9.1",
"mocha": "^2.5.3",
"mocha-jsdom": "^1.1.0",
"mocha-teamcity-reporter": ">=0.0.1",
"nock": "^9.0.0",
"node-localstorage": "^1.3.0",
"portscanner": "^2.1.0",
"proxyquire": "^1.7.10",
"selenium-server": "^2.53.0",
"selenium-webdriver": "^2.53.2",
"semver": "^5.3.0",
"serve-static": "^1.11.1",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.7.0"
},
"dependencies": {
"config": "^1.21.0"
}
}
我在world.js中创建了两个服务器。一个服务器运行在端口23661上的库和一个服务器,其中伪api在端口3000上。这可以在下面看到
'use strict';
const SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
const webdriver = require('selenium-webdriver');
const server = new SeleniumServer(require('selenium-server').path, {port: 4444});
const serveStatic = require('serve-static');
const express = require('express');
const chaiAsPromised = require('chai-as-promised');
const chai = require('chai');
const jsonServer = require('json-server');
const cors = require('cors');
chai.should();
chai.use(chaiAsPromised);
const testServer = jsonServer.create();
const router = jsonServer.router('db.json');
testServer.use(cors());
testServer.options('*', cors());
testServer.use(router);
testServer.listen(3000);
const app = express();
const httpServerPort = 23661;
app.use(cors());
app.options('*', cors());
app.use(serveStatic('dist'));
app.use(serveStatic(__dirname + '/../page'));
app.use(serveStatic('node_modules/sinon/pkg'));
app.listen(httpServerPort);
server.start();
运行此命令时,它会在firefox浏览器中正确运行验收测试。
我要拨打的文件
process.env.NODE_ENV
在我最小化的库中调用myLib.min.js。
这是未分类文件的一部分
'use strict';
const fetch = require('isomorphic-fetch');
module.exports = id => {
var apiUrl = 'http://liveurl.com/api/' + id;
if (process.env.NODE_ENV === 'development') {
apiUrl = 'http://localhost:3000/api';
}
console.log(process.env.NODE_ENV);
return fetch(apiUrl)
.then(response => {
if (response.status === 200) {
return response.json()
.then(myResponse => {
var theResponse = myResponse.id;
return theResponse;
});
} else {
return response.json().then(error => {
throw new Error(error.error);
});
}
});
};
答案 0 :(得分:1)
这通常通过使用environment variables来解决。
类似的东西:
var apiUrl = 'http://localhost:3000/api'
if (process.env.NODE_ENV === 'production') {
apiUrl = 'http://liveurl.com/api';
}
return fetch(apiUrl)
.then(response => {
//... and so on
然后你会像这样启动你的应用程序(或运行测试),为NODE_ENV填写适当的环境名称。常见的值是生产|测试|
NODE_ENV=production node ./path/to/app.js