简体   繁体   English

Node JS单元测试sinon chai

[英]Node js Unit test sinon chai

How to create unit test for the bellow file 如何为波纹管文件创建单元测试

index.ts 索引

import ----
import ----

let workerService = new WorkerService();
let creatWorkerPromise = Promise.promisify(workerService.createWorkers);

creatWorkerPromise()
.then(function(){
    let kafkaService =  new KafkaService(kafkaConfig, workerService);
})
.catch(function (err: any) {
    console.log('Error while creating workers:', err);
});

It's kind of difficult to test something that is not a unit, as a unit usually exposes methods that are to be tested, so I am assuming that you do this eventually in the test code. 测试不是单位的东西有点困难,因为单位通常会公开要测试的方法,所以我假设您最终会在测试代码中这样做。 So here goes: 因此,这里去:

 /////////// // index.js /////////// // stubs for this example to work var createWorkerPromise = function(){ return Promise.resolve(); } var workerService, kafkaConfig; function KafkaService(){ throw new Error("crashes test"); } function exportedFunction(){ return createWorkerPromise() .then(function(){ var kafkaService = new KafkaService(kafkaConfig, workerService); }) .catch(function (err) { console.log('Error while creating workers:', err); }); } /////////// // test.js /////////// var log = console.log.bind(console); var assert = function(expr) { if(!expr) throw new Error("AssertionError"); log("TEST OK"); } // the test var spy = sinon.spy(console, 'log'); exportedFunction().then(function test() { assert(spy.called); }).catch(log.bind(null, "TEST FAILED")) 
 <script src="https://unpkg.com/sinon@latest/pkg/sinon.js"></script> 

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

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