本文主要是介绍第8章-使用Express.js和Hapi构建Node.js-REST-API服务-8.2.项目依赖,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、项目依赖
现在开始我们的项目,
把依赖写入到package.json文件中
{"name": "rest-express","version": "0.0.1","description": "REST API application with Express, Mongoskin, MongoDB, Mocha and Superagent","main": "index.js","directories": {"test": "test"},"scripts": {"test": "mocha test -R spec"},"author": "LiJian","license": "BSD","dependencies": {"express": "4.1.2","mongoskin": "1.4.1","body-parser": "1.0.2","morgan": "1.0.1"},"devDependencies": {"mocha": "1.16.2","superagent": "0.15.7","expect.js": "0.2.0"}
}
执行命令安装:
$npm install
二、使用Mocha和Superagent进行测试
在实现应用之前,我们首先来编写测试用例,用来测试将要实现的REST API服务器。
在TDD模式中,可以借助这些测试用例来创建一个脱离Node.js的JSON REST API服务器,
这里会使用到Express.js框架和操作MongoDB的Mongoskin库
我们借助Mocha和Superagent库,通过发送HTTP请求到服务器执行基本的CURD操作
我们使用Mocha作为命令行工具,然后用Express.js和superagent作为本地库。
用下面的命令安装Mocha CLI(如果不行的话请参考$mocha -V),
在终端运行下面这行命令:
$npm install -g mocha@1.16.2
提示:我们可以把Mocha库安装到项目文件夹中,这样便可以在不同的项目中使用不同版本的Mocha,在测试时只需要进入
./node_modules/mocha/bin/mocha
目录即可。还有一种更好的办法,就是使用Makefile
我们创建一个文件test/index.js,他将包含6个测试用例:
1.创建一个新对象
2.通过对象ID检索对象
3.检索整个集合
4.通过对象ID更新对象
5.通过对象ID检查对象是否更新
6.通过对象ID删除对象
superagent的链式函数使发送HTTP请求变成一件很容易的事,这里每个用例中都会用到。
文件从引入依赖模块开始:
var superagent = require('superagent')
var expect = require('expect.js')describe('express rest api server', function(){var idit('post object', function(done){superagent.post('http://localhost:3000/collections/test').send({ name: 'lijian',email: '374452668@qq.com'}).end(function(e,res){ // console.log(res.body)expect(e).to.eql(null)//1.返回的错误对象需要为空expect(res.body.length).to.eql(1)//2.响应对象的数组应该含有且只含有一个元素expect(res.body[0]._id.length).to.eql(24)//第一个响应对象中应该包含一个24字节长度的_id属性,他是一个标准的MongoDB对象ID类型id = res.body[0]._id//将新创建的对象的ID保存到全局变量中,便于后续使用done()//测试异步代码不要漏掉这个函数,否则Mocha的测试程序会在收到服务器响应之前结束})})it('retrieves an object', function(done){superagent.get('http://localhost:3000/collections/test/'+id).end(function(e, res){ // console.log(res.body)expect(e).to.eql(null)expect(typeof res.body).to.eql('object')expect(res.body._id.length).to.eql(24)expect(res.body._id).to.eql(id)done()})})it('retrieves a collection', function(done){superagent.get('http://localhost:3000/collections/test').end(function(e, res){ // console.log(res.body)expect(e).to.eql(null)expect(res.body.length).to.be.above(0)expect(res.body.map(function (item){return item._id})).to.contain(id)done()})})it('updates an object', function(done){superagent.put('http://localhost:3000/collections/test/'+id).send({name: '深情小建',email: '374452668@qq.com'}).end(function(e, res){ // console.log(res.body)expect(e).to.eql(null)expect(typeof res.body).to.eql('object')expect(res.body.msg).to.eql('success')done()})})it('checks an updated object', function(done){superagent.get('http://localhost:3000/collections/test/'+id).end(function(e, res){ // console.log(res.body)expect(e).to.eql(null)expect(typeof res.body).to.eql('object')expect(res.body._id.length).to.eql(24)expect(res.body._id).to.eql(id)expect(res.body.name).to.eql('Peter')done()})})it('removes an object', function(done){superagent.del('http://localhost:3000/collections/test/'+id).end(function(e, res){ // console.log(res.body)expect(e).to.eql(null)expect(typeof res.body).to.eql('object')expect(res.body.msg).to.eql('success')done()})})
})
现在我们来运行这个测试,在命令行中运行$mocha test/index.js
或者npm test
。
不过得到的结果一定是失败,因为服务器还没有启动。
如果有多个项目,需要使用多个版本的Mocha,那么可以把Mocha安装到项目目录的node_modules文件夹下,然后执行:
./node_modules/mocha/bin/mocha ./test
注意:默认情况下,Mocha只返回少量的信息,如果需要得到更信息的结果,可以使用-R<name>
参数(即:$mocha test -R spec或者$mocha test -R list
)
这篇关于第8章-使用Express.js和Hapi构建Node.js-REST-API服务-8.2.项目依赖的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!