简体   繁体   English

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

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

I've been designing a javascript controller file and I want to testing if the instance of my controller have a instance of the express's method 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');
    }
}

and my controller 和我的控制器

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();

My question is: This is a valid way check an instance of the express router??? 我的问题是: 这是检查快速路由器实例的有效方法???

There's no need to duck-type Express router. 无需使用鸭子式Express路由器。

express.Router isn't a constructor and doesn't establish normal prototype chain that can be detected with instanceof . express.Router不是构造函数,并且未建立可用instanceof检测到的常规原型链。 Instead, it establishes prototype chain manually via hacky mixin technique . 相反,它通过hacky mixin技术手动建立原型链

It can be tested with 可以用

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

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

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