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. startActivityForResult()的用法

    举例说我想要做的一个事情是,在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Act ...

  2. 【单词】常见单词含义的辨异(emulator/simulator、hardware/firmware)

    1. emulator 与 simulator The Simulator tries to duplicate the behavior of the device.(仿真的是行为): The Em ...

  3. popover弹出框

    <style> #view{width: 300px;height: 200px;border: 1px solid red;} </style> 以上是为了viewport更 ...

  4. Mybaits中session的应用一

    获取一级缓存session SqlSession session = this.yangchebaoDbManagerImpl.getSqlSessionFactory().openSession(f ...

  5. android中常见声音操作方式(Ringtone,SoundPool,MediaPlayer)小结

    在Android开发中有时候需要用到播放声音操作,在android API 的media包中有三种方式可供我们选择,它们分别是Ringtone,SoundPool,MediaPlayer.因为在我目前 ...

  6. javascript: with 表单验证

    <html> <head> <script type="text/javascript"> function validate_required ...

  7. angular反向代理

    第一步:根目录新建 proxy.conf.json target:就是代理的服务器地址. 接口地址必须是http://localhost:8081/api开头 { "/api":{ ...

  8. Java缓存组件 EhCache 入门教程

    1.技术背景: 系统缓存是位于应用程序与物理数据源之间,用于临时存放复制数据的内存区域,目的是为减少应用程序对物理数据源访问的次数,从而提高应用程序的运行性能.缓存设想内存是有限的,缓存的时效性也是有 ...

  9. 如何去掉bootstrap table中表格样式中横线竖线

    修改之前,表格看上去比较拥挤,因为bootstrap table插件中自带斑马线表格样式,有横线和竖线分栏,现在我们不需要这些. 应UI设计的要求,要去掉中间的横线和竖线,使用了修改需求中一种简单粗暴 ...

  10. mybatis 嵌套查询子查询column传多个参数描述

    https://my.oschina.net/softwarechina/blog/375762