测试在Travis CI上使用Node.js(Express)构建的REST API [英] Testing REST api built using Node.js(Express) on Travis CI

查看:74
本文介绍了测试在Travis CI上使用Node.js(Express)构建的REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我在Travis CI上使用Express框架和MongoDb创建的其余api服务.该测试在我的计算机上本地运行,但在Travis CI上失败.我猜问题出在我的.travis.yml和package.json上.

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.

这是我在本地运行测试的方式:

// 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 

我如何在travis CI上执行相同的操作?这是我的应用程序 https://github.com/mujuni88/Express-RestApi-Tutorial

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"
    }

}

推荐答案

答案很简单-如果您未在脚本步骤中指定此服务器,Travis将不会为您启动服务器.看看我的存储库(有点复杂,但是,它确实在测试).

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).

您可以将express.js的需求添加到express.test.js中,然后从那里启动服务器.完成所有测试后,请不要忘记将其拆下.

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.

这篇关于测试在Travis CI上使用Node.js(Express)构建的REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆