Jasmine test: how to call through function that is called in tested function?

半腔热情 提交于 2019-12-13 06:51:17

问题


I try to test a JavaScript function with Jasmine. It looks like this:

show: function(){
    this.work();
    var mod = new Module();
    mod.doSomething();
}

I am now testing if work() is called in show(), which is no problem. But then an error occurs in mod.doSomething(). This makes my test fail although it actually passed. What can I do to prevent mod.doSomething to be called or at least executed. Is there something like callThrough() for nonused function calls?

The only idea I had was to write a global variable that can be set true or false from everywhere. Then I extend the mod.doSomething() function for not to be called if this variable is set true. Now I can simply set the variable to true, if I do not want this function to be executed while testing. But I think there must be a better solution. Can anybody help me?


回答1:


this is not testable with "standard" jasmine-practices. the method show() should take a module as parameter.... then it would be testable with a spy on doSomething()...



来源:https://stackoverflow.com/questions/29745532/jasmine-test-how-to-call-through-function-that-is-called-in-tested-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!