简体   繁体   中英

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?

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() ...

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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