繁体   English   中英

Mocha:测试express.Router实例的方法

[英]Mocha: Approach to Testing instance of express.Router

我一直在设计一个javascript控制器文件,我想测试我的控制器实例是否具有express方法Router()的实例。

import {expect} from 'chai';
import {UF_Controller} from '../../controllers/uf.controller.js';

describe('UF Controller', UFControllerDescription);

function UFControllerDescription(){
    it('1. Should be have a express router instance', spec1);

    function spec1(){
        expect(UF_Controller.router).itself.to.respondTo('get');
        expect(UF_Controller.router).itself.to.respondTo('post');
        expect(UF_Controller.router).itself.to.respondTo('put');
        expect(UF_Controller.router).itself.to.respondTo('delete');
    }
}

和我的控制器

import express from 'express';
import { uf } from '../models/uf.model';

class UFController{

    constructor(){
        this.router = express.Router();
        this.router.get('/', this.getAll);
  }

    getAll(req, res, next){
        res.json(uf.todayUF());
    }

    getUF(req, res, next){
    }

    insertUF(req, res, next){
    }

    replaceUF(req, res, next){
    }

    updateUF(req, res, next){
    }

    deleteUF(req, res, next){
    }
}

export const UF_Controller = new UFController();

我的问题是: 这是检查快速路由器实例的有效方法???

无需使用鸭子式Express路由器。

express.Router不是构造函数,并且未建立可用instanceof检测到的常规原型链。 相反,它通过hacky mixin技术手动建立原型链

可以用

expect(Object.getPrototypeOf(UF_Controller.router)).to.equal(express.Router);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM