茉莉花测试:如何通过测试函数中调用的函数调用? [英] Jasmine test: how to call through function that is called in tested function?

查看:117
本文介绍了茉莉花测试:如何通过测试函数中调用的函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用Jasmine测试JavaScript函数。它看起来像这样:

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

$ / code>

我现在正在测试 work() code>在 show()中调用,这是没有问题的。但是,在 mod.doSomething()中发生错误。这使我的测试失败,虽然它实际上已经通过。我能做些什么来防止mod.doSomething被调用或至少被执行。是否有像 callThrough()用于未使用的函数调用?



我唯一的想法是编写一个全局从任何地方都可以设置为true或false的变量。然后,我扩展了 mod.doSomething()函数,以便在此变量设置为true时不被调用。现在我可以简单地将变量设置为true,如果我不想在测试时执行此函数。但我认为必须有更好的解决方案。任何人都可以帮助我吗?

解决方案

这不适用于标准茉莉花做法。方法 show()应该将一个模块作为参数....然后它可以在 doSomething() ...


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?

解决方案

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

这篇关于茉莉花测试:如何通过测试函数中调用的函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆