问题
I want to test the rest api service i created using Express framework and MongoDb on Travis CI. The test works locally on my machine but fails on Travis CI. I'm guessing the problem is with my .travis.yml and package.json.
Here is how i run my test locally:
// opens mongodb database on port 27017
mongod
// reads the db and returns documents depending on the request
node express.js
// test CRUD operations on the app
mocha express.test.js
How can i do the same on travis CI? Here is my app https://github.com/mujuni88/Express-RestApi-Tutorial.
travis.yml:
language: node_js
node_js:
- "0.10"
- "0.9"
services: mongodb
script:
- "./node_modules/mocha/bin/mocha *.test.js"
package.json:
{
"name":"express-restapi",
"author":"Joe Buza",
"description":"Express RestApi",
"version": "0.0.1",
"private": true,
"keywords":["angular", "mocha", "mongodb", "express"],
"license":"BSD",
"dependencies": {
"express":"3.4.4",
"mongoskin":"0.6.0"
},
"devDependencies":{
"mocha":"1.13.0",
"expect.js":"0.2.0",
"superagent":"0.15.7"
},
"scripts":{
"start":"node express.js"
}
}
回答1:
The answer is simple - Travis will not start up your server for you, if you not specify this in scripts steps. Look at my repository (it's a bit complicated, but hey, it does testing).
You can add require of express.js into express.test.js and start up server from there. After all tests is completed do not forget to tear it down.
来源:https://stackoverflow.com/questions/19719697/testing-rest-api-built-using-node-jsexpress-on-travis-ci