简体   繁体   English

JavaScript Jasmine单元测试范围

[英]JavaScript Jasmine unit test coverage

Folks, Lets say I have the following function. 伙计们,可以说我有以下功能。 What would be a proper way to write a Spy, or any other method of testing this with 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); });
};

Thanks! 谢谢!

If you want to test if the function has been called or with what arguments it was called you can use jasmine.createSpy() ... 如果要测试是否已调用该函数或使用什么参数调用该函数,可以使用jasmine.createSpy() ...

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

If you want to test the return result of the function you can just call it in your expect... 如果要测试该函数的返回结果,可以按期望的方式调用它...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM