如何在Array.some中产生

时间:2016-11-04 11:22:03

标签: node.js asynchronous

我有一个生成器函数,它返回一个这样的函数,

*getClassifier(classifier) {
    if (!classifier) {
        return async(function *() {
            return true;
        });
    }

    if (classifier !== null && typeof classifier === "object") {
        return false;
    }

    let name = classifier.name;
    if (!this._classRegistry[name])
        throw new Error("Classifier " + name + " is not registered");
    let com = yield this.injector.resolve(this._classRegistry[name]);

    return (message) => com.classify(message, classifier.options);
}

我对这个函数进行单元测试,看起来像这样

result = yield someclass.getClassifiers(classifier)(message);

然后我对如何使用chai期待结果感到困惑 示例expect(result).to.be.true;

任何提示或解决方案都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我不太确定你要在这里完成什么,但是你可以试试chai-generator模块,它允许你做一些像(来自文档)的事情:

expect(generator).to.yield(true);

顺便说一下,我认为你以错误的方式使用yield,从yield函数中使用yield来产生值。从外部,您必须使用迭代器来遍历生成器函数生成的所有值。

你在第二段代码中使用yield关键字对我没有意义。此外,我不明白为什么要从发电机返回值。在这种情况下,yield *value*应该替换return *value*