了解 Sinon.js 的 stub.yield()

Understanding Sinon.js's stub.yield()

stub.yield([arg1, arg2, ...])

Invoke callbacks passed to the stub with the given arguments. If the stub was never called with a function argument, yield throws an error. Also aliased as invokeCallback.

如果从未使用函数参数调用存根”是什么意思?

这意味着如果调用了存根,但给定参数中没有函数,则会抛出异常。

考虑这个存根:

sinon.stub(fs, 'readFile');

现在测试运行这些行:

fs.readFile('some-file'); // no callback passed
fs.readFile.yield(); // throws, because the stub was never invoked with a function