Tag: 摩卡咖啡的

使用sinon和mocha来testingnode.js http.get

假设我有以下function 'use strict'; var http = require('http'); var getLikes = function(graphId, callback) { // request to get the # of likes var req = http.get('http://graph.facebook.com/' + graphId, function(response) { var str = ''; // while data is incoming, concatenate it response.on('data', function (chunk) { str += chunk; }); // data is fully recieved, and now […]

不同的参数/返回值为sinon期望

我正在为我的模块编写unit testing,并使用SinonJS来validation对其他模块的函数调用的期望。 首先,我为其他模块注册一个模拟: var otherModule = { getConfig : function () {} }; mockery.registerMock("otherModule", otherModule); 之后,我运行一些testing,并(成功)validation一些期望,如: var otherModuleMock = sinon.mock(otherModule); otherModuleMock .expects("getConfig") .once() .withArgs("A") .returns(configValuesForA); // run test otherModuleMock.verify(); // <- succeeds 我碰到一个问题,但是,当模块调用getConfig函数两次,不同的参数: otherModuleMock .expects("getConfig") .once() .withArgs("A") .returns(configValuesForA); otherModuleMock .expects("getConfig") .once() .withArgs("B") .returns(configValuesForB); 从我对文档的理解,这应该可能工作。 但是,这会导致以下错误: ExpectationError: Unexpected call: getConfig(A) Expectation met: getConfig(A[, …]) once Expectation […]