本文介绍了使用 Jasmine 监视函数中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有如下函数
function fun1(a) {
var local_a = a;
local_a += 5;
return local_a/2;
}
有没有办法测试 local_a 的值是什么(例如在第一行代码中)?我对茉莉花有点陌生,所以我被卡住了.提前致谢.
Is there a way to test for the value of local_a being what it should be (for example in the first line of code)? I'm a bit new to Jasmine, so am stuck. Thanks in advance.
推荐答案
不是.不过,您可以执行以下操作:
Not really. You can do the following though:
测试fun1()
的结果:
expect(fun1(5)).toEqual(5);
确保它被实际调用(如果它通过事件发生则很有用)并测试结果:
Make sure it's actually called (useful if it happens through events) and also test the result:
var spy = jasmine.createSpy(window, 'fun1').andCallThrough();
fire_event_calling_fun1();
expect(spy).toHaveBeenCalled();
expect(some_condition);
真正重现检查中间结果的整个功能:
Really reproduce the whole function inspecting intermediate results:
var spy = jasmine.createSpy(window, 'fun1').andCallFake(function (a) {
var local_a = a;
expect(local_a).toEqual(a);
local_a += 5;
expect(local_a).toEqual(a+5);
return local_a/2;
});
fun1(42);
expect(spy).toHaveBeenCalled();
这篇关于使用 Jasmine 监视函数中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!