Lad*_*ein 2 javascript node.js jasmine nodeunit
我在我的node.js应用程序中有一个JS方法,我想进行单元测试.它对服务方法进行多次调用,每次都将该服务传递回调; 回调累积结果.
我如何使用Jasmine来删除服务方法,以便每次调用存根时,它都会使用由参数确定的响应来调用回调?
这是(就像)我正在测试的方法:
function methodUnderTest() {
var result = [];
var f = function(response) {result.push(response)};
service_method(arg1, arg2, f);
service_method(other1, other2, f);
// Do something with the results...
}
Run Code Online (Sandbox Code Playgroud)
我想指定当使用arg1和arg2调用service_method时,存根将使用特定响应调用f回调,并且当使用other1和other2调用它时,它将使用不同的特定响应调用相同的回调.
我也考虑一个不同的框架.(我尝试过Nodeunit,但没有按照我的意愿去做.)
Gre*_*egg 11
你应该能够使用callFake
间谍策略.在jasmine 2.0中,这看起来像:
describe('methodUnderTest', function () {
it("collects results from service_method", function() {
window.service_method = jasmine.createSpy('service_method').and.callFake(function(argument1, argument2, callback) {
callback([argument1, argument2]);
});
arg1 = 1, arg2 = 'hi', other1 = 2, other2 = 'bye';
expect(methodUnderTest()).toEqual([[1, 'hi'], [2, 'bye']]);
});
});
Run Code Online (Sandbox Code Playgroud)
当methodUnderTest
返回结果数组.