zoukankan      html  css  js  c++  java
  • [Node.js] Test Node RESTful API with Mocha and Chai

    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();
            });
      });
    });
  • 相关阅读:
    Aix_bugzilla
    aix Mysql安装 Oracle官方教程
    Aix6.1安装openssh
    日媒:阿里巴巴上市融资或超Facebook
    设计模式(一)---单例模式
    Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释
    TCP/IP协议族——IP工作原理及实例具体解释(上)
    leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
    Material Design之RecyclerView的使用(一)
    jQuery和CSS3超酷表单美化插件
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6784172.html
Copyright © 2011-2022 走看看