本文介紹了使用 Jasmine 監視函數中的變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
假設我有如下函數
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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!