Using Jasmine to spy on variables in a function(使用 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 监视函数中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Jasmine 监视函数中的变量


基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01