Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个项目,它从我想测试的 node.js 服务器对 Twitter 的 API 进行一些调用。但是,我不想测试 Twitter API(OAuth 和我正在调用的实际 API),所以我认为最好将它存根。
我发现 sinon.js 据说支持这一点。我还没有找到如何执行此操作的示例,并且想知道之前是否已完成此操作。如果是这样,查看一些示例代码会很有帮助。
你在做什么来调用 API?
想象一下,您使用一个名为的方法api.call('url', function (error, response) {...}):
api.call('url', function (error, response) {...})
您可以用 sinon 轻轻地或任何您喜欢的方式“伪造”回调。
轻轻使用示例:
gently.expect(api, 'call', function (url, callback) { assert.equal(url, 'http://api.twitter.com/...'); callback(Error('Fake error'), null); });