JavaScript Jasmine单元测试覆盖率

时间:2014-07-14 14:47:40

标签: javascript jasmine

民间,   可以说我有以下功能。什么是写一个间谍的正确方法,或用Jasmine测试它的任何其他方法?

var Ideas = require('../models/Ideas').Ideas;

var doSomething = function doSomething(req, rsp, userId) {
    controllerHelper.batchGet(req, rsp,
        function(ids) { return Ideas.get(ids, userId); },
        function(tags) { return Ideas.getTags(tags, userId); },
        function(tags) { return Ideas.getSpecificTags(tags, userId); },
        function() { return Ideas.getAll(userId); });
};

谢谢!

1 个答案:

答案 0 :(得分:1)

如果你想测试函数是否被调用或者调用了什么参数,你可以使用jasmine.createSpy() ......

it("should test your function", function () {        
    doSomething = jasmine.createSpy();
    doSomething(1,2,3);
    expect(doSomething).toHaveBeenCalled();
    expect(doSomething).toHaveBeenCalledWith(1,2,3);
});

如果你想测试函数的返回结果,你可以在期望中调用它......

it("should test your function", function () {                
    expect(doSomething(req, rsp, userId)).toEqual(expectedResult);
});
相关问题