假设我有如下函数
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 监视函数中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 javascript 认为文档“准备好"之前,如何让How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 认为文档“准备好
jasmine 运行和等待实际上是做什么的?What do jasmine runs and waitsFor actually do?(jasmine 运行和等待实际上是做什么的?)
如何提供模拟文件来更改 <input type='filHow to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模拟文件来更改 lt;input type=filegt; 的事
如何使用 Jasmine 对链式方法进行单元测试How to unit test a chained method using Jasmine(如何使用 Jasmine 对链式方法进行单元测试)
如何将 $rootScope 注入 AngularJS 单元测试?How do I inject $rootScope into an AngularJS unit test?(如何将 $rootScope 注入 AngularJS 单元测试?)
Jasmine - 如何监视函数中的函数调用?Jasmine - How to spy on a function call within a function?(Jasmine - 如何监视函数中的函数调用?)