Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript sinon spy没有';在生成器循环中不注册调用?_Javascript_Node.js_Mocha.js_Generator_Sinon - Fatal编程技术网

Javascript sinon spy没有';在生成器循环中不注册调用?

Javascript sinon spy没有';在生成器循环中不注册调用?,javascript,node.js,mocha.js,generator,sinon,Javascript,Node.js,Mocha.js,Generator,Sinon,我想检查是否调用了一段代码,所以我使用sinon spy来断言这一点。然而,尽管console.log显示代码被正确调用,spy似乎失败了 我想知道我作为一个发电机的功能是否导致我的间谍误报它在做什么 我的代码(为了简洁起见,我去掉了一些代码块): 还有我的测试: const Blacklist = require('../../src/Blacklist2'); const childProcess = require('child_process'); const uuid = requir

我想检查是否调用了一段代码,所以我使用sinon spy来断言这一点。然而,尽管console.log显示代码被正确调用,spy似乎失败了

我想知道我作为一个发电机的功能是否导致我的间谍误报它在做什么

我的代码(为了简洁起见,我去掉了一些代码块):

还有我的测试:

const Blacklist = require('../../src/Blacklist2');
const childProcess = require('child_process');
const uuid = require('uuid/v4');

describe('Blacklist', () => {
  let blacklist;
  beforeEach(() => {
    blacklist = new Blacklist(childProcess);
    blacklist.IS_BLACKLISTED_SCRIPT = './test/helpers/good.py';
  });
  describe('isBlacklisted', () => {
    it('should call the _errorEvent for every name in a release when the blacklist application is not available', async () => {
      let release = {
        id: 1001,
        asset_controller: {
          id: 54321,
        },
        display_name: 'Blah',
        names: [
          {
            id: 2001,
            name: 'Blah',
          },
        ],
      };

      blacklist.IS_BLACKLISTED_SCRIPT = './test/helpers/'+ uuid() +'.py';


      const spy = sinon.spy(blacklist, '_errorEvent');
      blacklist.isBlacklisted(release, uuid());

      console.log(spy);
      sinon.assert.calledTwice(spy);
      spy.restore();
    });
  });
});
我的间谍报告:

不打电话:对


我将把我的评论扩展成一个实际的答案,希望能有所帮助


您的问题在于异步,而不是生成器。你需要被列入黑名单才能兑现你可以等待的承诺。否则,您的断言将在调用spy之前发生

大概是这样的:

isBlacklisted(release, jobUUID) {
    let promises = names.map((name) => {
      return this._spawnPythonProcessGenerator(
        this.IS_BLACKLISTED_SCRIPT,
        name
      ).next().value
        .then((data) => {
          console.log(data);
        })
        .catch((err) => {
          this._errorEvent(release, name, err, jobUUID);
        });
    }, this);

    return Promise.all(promises);
}
然后,在测试中:

return blacklist.isBlacklisted(release, uuid())
    .then(() => {
        sinon.assert.calledTwice(spy);
    });
还有。。。这与您的问题无关,但是您的
\u pythonprocessgenerator
方法不需要是生成器。通过这样调用
next
并为每个数组项重新调用整个过程,您只使用它的第一个值

如果您取出
*
,将
收益率
更改为
返回
,并在调用时跳过
.next().value
,则其工作原理相同。您可能还想重命名它,因为它不是生成器

_spawnPythonProcess(scriptSrc, name) {
    const pythonProcess = this._childProcess.spawn(
      'python3',
      [...arguments]
    );
    return new Promise((resolve, reject) => {
      pythonProcess.stderr.on('data', (err) => {
        reject(err.toString());
      });

      pythonProcess.stdout.on('data', (data) => {
        resolve(data.toString());
      });
    });
}
当你称之为:

let promises = names.map((name) => {
  return this._spawnPythonProcess(
    this.IS_BLACKLISTED_SCRIPT,
    name
  )
    .then((data) => {
      console.log(data);
    })
    .catch((err) => {
      this._errorEvent(release, name, err, jobUUID);
    });
}, this);

return Promise.all(promises);

嗯,测试建议它应该调用
\u errorEvent
获取名称的数量-测试正在检查2,但测试数据只有一个名称?啊,对不起@James,我取出了一些代码,但基本上名称是名称数组中的
display\u name
name
。如果names数组有11个条目,那么理论上可以调用12次
\u errorEvent
。即使如此,如果你是正确的,我的间谍应该报告
调用
,而不是
不调用
。你的问题在于异步,而不是生成器。您需要
被列入黑名单
才能返回您可以等待的承诺。否则,您的断言将在调用spy之前发生。@sripberger是对的,看起来不像是
IsBlackListed
正在传播来自
\u SprownPythonProcessGenerator
的承诺,因此您的测试无法等待承诺的解决。您可以通过将断言移动到
setTimeout
(这将涉及在回调中手动调用测试的
done
)来验证这一点。测试应该通过,您需要更新
isBlacklisted
以返回您可以在测试中处理的
承诺。为答案干杯。。。我必须承认我不记得为什么我把它做成了发电机
let promises = names.map((name) => {
  return this._spawnPythonProcess(
    this.IS_BLACKLISTED_SCRIPT,
    name
  )
    .then((data) => {
      console.log(data);
    })
    .catch((err) => {
      this._errorEvent(release, name, err, jobUUID);
    });
}, this);

return Promise.all(promises);