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. golang panic and recover

    panic 是一个内置函数,当一个函数 F 调用 panic,F 的执行就会停止,F 中 deferred 函数调用会被执行,然后 F 返回控制到它的调用者.这个过程会沿着调用栈执行下去,直到当前 g ...

  2. php基础篇之一

    1.PHP是什么 官方文档:超文本预处理器 2.PHP能够做一些什么? PHP主要应用在一下领域: (1)服务器端脚本,需要:PHP解析器,PHP服务器,PHP浏览器. (2)命令行脚本,只需要PHP ...

  3. C++编码优化之减少冗余拷贝或赋值

    临时变量 目前遇到的一些产生临时变量的情况:函数实参.函数返回值.隐式类型转换.多余的拷贝 1. 函数实参 这点应该比较容易理解,函数参数,如果是实参传递的话,函数体里的修改并不会影响调用时传入的参数 ...

  4. 洛谷——U10783 名字被和谐了

    https://www.luogu.org/problem/show?pid=U10783 题目背景 众所周知,我们称g是a的约数,当且仅当g是正数且a mod g = 0. 众所周知,若g既是a的约 ...

  5. C语言速度优化之指针赋值与if推断

    近期在写的一个项目须要优化处理速度,我写了一下程序来測试指针赋值与指针推断的速度比較.结果让我大吃一惊. #include <stdio.h> #include <stdlib.h& ...

  6. PHP用socket连接SMTP服务器发送邮件

    PHP用socket连接SMTP服务器发送邮件 PHP用socket连接SMTP服务器发送邮件学习实验记录: 分析与SMTP会话的一般流程 1. HELO XXX \r\n //XXX就是自己起个名字 ...

  7. select下拉列表选中后,跳转新链接

    1.在当前页面打开新链接 <select onchange="location.href=this.options[this.selectedIndex].value" na ...

  8. HDU 4508 湫湫系列故事——减肥记I (2013腾讯编程马拉松初赛第一场)

    http://acm.hdu.edu.cn/showproblem.php?pid=4508 题目大意: 给定一些数据. 每组数据以一个整数n开始,表示每天的食物清单有n种食物.  接下来n行,每行两 ...

  9. 【例题 6-12 UVA - 572 】Oil Deposits

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] dfs.. [代码] #include <bits/stdc++.h> using namespace std; con ...

  10. [Angular] Design API for show / hide components based on Auth

    Simple Auth service: import { Injectable } from '@angular/core'; import {HttpClient} from '@angular/ ...