最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to mock a function in Karma.js - Stack Overflow

programmeradmin0浏览0评论

Does somebody knows how to mock a function result with karma.js ? The function uses a var given by thymeleaf framework (java, spring boot, etc..).

function isFlooring() {
    var isMyChoiceOk = [[${mychoice}]];
    if(typeof isMyChoiceOk !== 'undefined') {
        return isMyChoiceOk;
    }
    else {
        return false;
    }
}

What I want to do is to tell karma.js that the result of this function is TRUE or FALSE.

Does somebody knows how to mock a function result with karma.js ? The function uses a var given by thymeleaf framework (java, spring boot, etc..).

function isFlooring() {
    var isMyChoiceOk = [[${mychoice}]];
    if(typeof isMyChoiceOk !== 'undefined') {
        return isMyChoiceOk;
    }
    else {
        return false;
    }
}

What I want to do is to tell karma.js that the result of this function is TRUE or FALSE.

Share Improve this question edited Jul 10, 2018 at 13:31 Mahozad 25.1k19 gold badges159 silver badges183 bronze badges asked Dec 21, 2017 at 17:05 uncleBountyuncleBounty 7838 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You can create a spy on the function (this assumes your function isn't part of an object) and then replace it with your own function:

spyOn(window, 'isFlooring')
    .and.callFake( function(arguments) {
         // return whatever you want to here
         return true
     }

The spy just listens for that function to be called and then 'callFake' replaces the functionality with what you want it to be.

If your function is part of an object, replace 'window' in the 'spyOn' call with the name of the object.

发布评论

评论列表(0)

  1. 暂无评论