我为我的图书馆创建了验收测试。当库发布时,它将使用尚未访问的外部API服务。因此,我创建了一个模拟的json服务器,它返回要测试的所需的JSON。要使测试通过,我必须做的是手动更改请求所在的文件中的API url。
我想知道是否有一种方法可以在运行验收测试时使用模拟API url,并在不运行验收测试时返回到活动URL。下面是带有活动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);
});
}
});当我只运行验收测试时,我想做的是更改url,'http://liveURL.com/api',我得到了从'http://localhost:3000/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上运行库的服务器和端口3000的伪api所在的服务器。这可以在下面看到
'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);
});
}
});
};发布于 2016-11-23 21:56:20
这通常通过使用环境变量来解决。
类似于:
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.jshttps://stackoverflow.com/questions/40774291
复制相似问题