简体   繁体   English

NodeJS单元测试

[英]NodeJS unit testing

Possibly a naive question but I have a class similar to(mine has more data read from database and caching them): 可能是一个幼稚的问题,但是我有一个类似的类(我从数据库中读取了更多数据并将其缓存):

var genericClass = class {

    constructor() {
        this.genericStructure = {};
    }

    async getState(key) {
        //some more core logic
        return this.genericStructure[key];
    }

    async putState(key, genericJson) {
        this.genericStructure[key] = genericJson;
        return true;
    }
}

its getState and putState are used multiple times in a consumer class, I want to mock all these occurences from a local map object in my test class. 它的getStateputState在使用者类中多次使用,我想从我的测试类中的本地map对象中模拟所有这些情况。 Can it be done? 能做到吗

I am using chai, mocha, sinon 我正在使用chai, mocha, sinon

"devDependencies": {
        "chai": "^4.1.2",
        "chai-as-promised": "^7.1.1",
        "eslint": "^4.19.1",
        "mocha": "^5.2.0",
        "nyc": "^12.0.2",
        "sinon": "^6.0.0",
        "sinon-chai": "^3.2.0"
    }

Instead of letting your class initialize the structure it wants, you should provide (inject) it the the structure on initialization. 与其让类初始化所需的结构,不如在初始化时为其提供(注入)结构。 This is the principle of Dependency Injection ( Constructor injection ). 这是依赖注入(构造函数注入)的原理。

var genericClass = class {

    constructor(initialStructure) {
        this.genericStructure = initialStructure;
    }

    async getState(key) {
        //some more core logic
        return this.genericStructure[key];
    }

    async putState(key, genericJson) {
        this.genericStructure[key] = genericJson;
        return true;
    }
}

And now in your tests you can pass whatever you like for initialStructure . 现在,在您的测试中,您可以通过任何您想要的initialStructure

Updated Answer 更新的答案

From your comment seems like the question was a little confusing. 从您的评论看来,这个问题有点令人困惑。 I think what you need is to mock the class methods as well to change their functionality, and at the same time support dependency injection. 我认为您需要模拟类方法以更改其功能,同时支持依赖项注入。 You can use something like below - 您可以使用如下所示的内容-

function testClassProxy(map) {

    let handleSetProps = function(args){
      map.set(args[0], args[1]);
      return true;
    }

    let handleGetProps = function(args){
      return map.get(args[0]);
    }

    let handler = {
        get(target, propKey) {
            return function (...args) {
                switch(propKey){
                  case 'setProps':{
                    return handleSetProps(args)
                  } 
                  case 'getProps': {
                    return handleGetProps(args)
                  }
                }
            };
        }
    };

    return new Proxy(map, handler);
}


let proxy = testClassProxy(new Map());


console.log(
  proxy.setProps('a', '1'),
  proxy.getProps('a')
);

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

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