使用 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();
相关文章