簡體   English   中英

使用Jasmine進行Ajax單元測試“ TypeError:無法讀取未定義的屬性'done'”

[英]Ajax unit test using Jasmine “TypeError: Cannot read property 'done' of undefined”

 function download() { $.ajax({ method: 'GET', url: 'http://jsonplaceholder.typicode.com/posts', }).done(function(data) { //processJasonData(data) }).fail(function () { //$fail(); }) } 

我試圖使用ajax使用Jasmine進行單元測試,但出現“ TypeError:無法讀取未定義的屬性'done'”。

 describe('AJAX check', function () { var url = "http://jsonplaceholder.typicode.com/posts"; it('test1', function () { spyOn($, "ajax"); download(url); expect($.ajax).toHaveBeenCalled(); }); }); 
有人知道如何解決這個問題嗎? 以及如何檢查ajax何時完成? 我想我應該在這種情況下使用承諾並推遲承諾,所以有人可以向我解釋嗎? (我使用jQuery> 1.5)

其余代碼:

 function download(url) { $.ajax({ method: 'GET', url: url, }).done(function(data) { processJasonData(data) }).fail(function () { $fail(); }) } function $fail() { var error_msg_1 = '<div class="jumbotron text-center"><h1 style="size: 10px;color: red"> Faill </h1></div>'; $('.tresc').html(error_msg_1); } function processJasonData(data) { var _data = $('.panel-group'); var dataTemplate = $('#item_tmp').html(); $.each(data, function (i, item) { _data.append(Mustache.render(dataTemplate, item)); }); } 

茉莉花spyOn阻止對該函數的調用,並將其“吞下”(返回未定義)。 要使其傳遞到spied函數的調用,請使用spyOn($, 'ajax').and.callTrough() 參見茉莉花文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM