【问题标题】:Mocha: Approach to Testing instance of express.RouterMocha: 测试 express.Router 实例的方法
【发布时间】:2016-12-19 23:10:43
【问题描述】:

我一直在设计一个 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();

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

【问题讨论】:

    标签: javascript unit-testing ecmascript-6 mocha.js


    【解决方案1】:

    不需要duck-type Express路由器。

    express.Router 不是构造函数,也没有建立可以用instanceof 检测到的正常原型链。相反,它是establishes prototype chain manually via hacky mixin technique

    可以测试

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

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      相关资源
      最近更新 更多