In this lesson, we will use Chai's request method to test our Node application's API responses.
By the end of this lesson, you will know how to:
- install the prerequisites to use mocha and chai in your application
- test for HTTP status response codes
- test for a string of text on a page
- test for a json response and validate the properties of the object
- write tests that not only verify the response of your application, but the behavior as well

  const mockRouter = require('./routes/mock');
app.use('/mock', mockRouter);
// routers/mock.js

const express = require('express');
const router = express.Router(); router
.get('/', (req, res, next) => {
res.status(200)
.json({ title: 'Mock test' })
})
.post('/', (req, res, next) => { const { v1, v2 } = req.body;
if (isNaN(Number(v1)) || isNaN(Number(v2))) {
res.status(400)
.json({ 'msg': 'You should provide numbers' });
} else {
const result = Number(v1) + Number(v2);
res.status(200)
.json({ result });
}
}); module.exports = router;
// test/mock_test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const should = chai.should();
const server = require('../../src/app'); chai.use(chaiHttp); describe('/mock GET', () => {
it('should return json', (done) => {
chai.request(server)
.get('/mock')
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('title')
.and
.that
.equal('Mock test');
done();
})
}); it('should return right value', (done) => {
chai.request(server)
.post('/mock')
.set('content-type', 'application/json')
.send({
v1: 2,
v2: 3
})
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('result').that.equals(5);
done();
});
}); it('should return 400 error', (done) => {
chai.request(server)
.post('/mock')
.set('content-type', 'application/json')
.send({
v1: 'tow',
v2: 'three'
})
.end((err, res) => {
res.should.have.status(400);
res.body.should.have.property('msg').that.contains('provide numbers');
done();
});
});
});

[Node.js] Test Node RESTful API with Mocha and Chai的更多相关文章

  1. 【重学Node.js 第1&2篇】本地搭建Node环境并起RESTful Api服务

    本地搭建Node环境并起RESTful Api服务 课程介绍看这里:https://www.cnblogs.com/zhangran/p/11963616.html 项目github地址:https: ...

  2. .NET程序员也学Node.js——初识Node.js

    清明在石门休了八天假,一眨眼,4月又到中旬了...看到.NET在天朝彻底沦陷而又无能为力,我开始尝试去学习一些新的东西来充实自己,我自然是打死不会去学java的,没有为什么,于是乎,最近开始学习一些前 ...

  3. 一起来学node.js吧 node school简介

    node.js这几年火爆的简直丧心病狂,去lagou.com查查node.js的职位,那叫一个多. 要说火爆到什么程度,竟然有一个网站专门去教大家学习node.js, Node School. 进去逛 ...

  4. 基于Node的PetShop,RESTful API以及认证

    前篇 - 基本认证,用户名密码 后篇 - OAuth2 认证 由于宠物店的业务发展需要,我们需要一种更加便捷的方式来管理日益增多的宠物和客户.最好的方法就是开发一个APP,我可以用这个APP来添加.更 ...

  5. 笔记-Node.js中的核心API之HTTP

    最近正在学习Node,在图书馆借了基本关于Node的书,同时在网上查阅资料,颇有收获,但是整体感觉对Node的理解还是停留在一个很模棱两可的状态.比如Node中的模块,平时练习就接触到那么几个,其他的 ...

  6. node.js学习二---------------------同步API和异步API的区别

    /** * node.js大部分api都有同步的方法,同步方法名后面都会带有Sync,js编译的时候,同步代码会立即执行,异步代码会先存到异步池中,等同步代码执行完后它才会执行异步:不会阻塞线程,没有 ...

  7. What are some advantages of using Node.js over a Flask API?

    https://www.quora.com/What-are-some-advantages-of-using-Node-js-over-a-Flask-API Flask is a Python w ...

  8. Node.js入门-Node.js 介绍

    Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...

  9. 极简 Node.js 入门 - Node.js 是什么、性能有优势?

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

随机推荐

  1. 一个理性战胜感性的成功案例:P2P投资和活期理财,纠结中提炼出来的1个数学问题

    我经常是投资了P2P,然后用钱,因而损失了一部分收益. 这是一个让我纠结的问题,为了解决这个问题,我不再凭感觉,而是从现实情况,提炼出来1个数学题,解答我的疑惑. 这是一个理性战胜感性的成功案例~ P ...

  2. 文件上传流式处理commons-fileupload

    1. 从请求中获取MultipartFile @RequestMapping(value="/upload", method=RequestMethod.POST) public ...

  3. background-size在PC端和移动端使用媒体查询的不同

    1.PC端background-size:100%:是展现原图的大小. 2.使用媒体查询的移动端的background-size:100%:是根据内容的高度自动拉伸高度的.

  4. 前端面试题(VUE)

    (前端面试题大全,持续更新) vue:v-model 双向绑定的原理 依赖收集 虚拟DOM的作用 vue@3.0中的preset配置? 父组件A和其子组件B/子组件C,B/C进行通信的方式(怎么通信) ...

  5. 学习Java必看书籍和步骤

    Java语言基础  谈到Java语言基础学习的书籍,大家肯定会推荐Bruce Eckel的<ThinkinginJava>.它是一本写的相当深刻的技术书籍,Java语言基础部分基本没有其它 ...

  6. Altium Designer规则的制定,一般规则

    资源来源于网上: 1,线间距6~10个mil 2,铺铜间距 20mil 实心 3,焊盘对焊盘间距10mil 4,测量电压的位置要到引脚附近. 敷铜:

  7. jmeter--错误之Not able to find Java executable or version. Please check your Java installation. errorlevel=2

    学习jmeter中遇到的问题: 'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件. Not able to find Java executable or version. Pl ...

  8. JNI之——Can't load IA 32-bit .dll on a AMD 64-bit platform错误的解决

    转载自:http://blog.csdn.net/l1028386804/article/details/46605003 在JNI开发中,Java程序需要调用操作系统动态链接库时,报错信息:Can' ...

  9. sprinng in action 第四版第六章中的ValidationMessages.properties不起作用

    文件名必须是ValidationMessages.properties,必须放在类的根目录下

  10. 每日技术总结:setInterval,setTimeout,文本溢出,小程序,wepy

    前言: 项目背景:vue,电商,商品详情页 1.倒计时,倒计到0秒时停止 data () { return { n: 10 } }, created () { let int = setInterva ...