久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 jasmine 測試完成和失敗的延遲對象

How to test the done and fail Deferred Object by using jasmine(如何使用 jasmine 測試完成和失敗的延遲對象)
本文介紹了如何使用 jasmine 測試完成和失敗的延遲對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

這里是關于 javascript 提交請求的代碼 (1).
下面是關于使用jasmine模擬ajax請求的測試(2).

Here is the code about the javascript submit request (1).
Here is the test about mocking the ajax request by using jasmine (2).

我想模擬服務器行為.有什么想法嗎?
有關詳細信息,請參閱 (1) 和 (2) 中的注釋.

I would like to mock the server behaviour. Any ideas?
See the comment in (1) and (2) for more details.

附:
實際上在這兩種情況下,fakeFunction 的 done 和 fail 延遲對象都會被調用.

P.S.:
Actually in both case the done and the fail Deferred Object of fakeFunction are called.

(1)

submitForm: function () {
     // the server execute fail only if message.val() is empty
     // and I would like to mock this behaviour in (2)
     backendController.submitForm(message.val()).done(this.onSuccess).fail(this.onError);
},

backendController.submitForm = function (message) {
    return $.ajax({
        url: 'some url',
        type: 'POST',
        dataType: 'json',
        data: {
            message: message
        }
    }).done(function () {
        //some code;
    });
};

<小時>

(2)

describe('When Submit button handler fired', function () {
    var submitFormSpy,
        fakeFunction = function () {
            this.done = function () {
                return this;
            };
            this.fail = function () {
                return this;
            };
            return this;
        };

    beforeEach(function () {
        submitFormSpy = spyOn(backendController, 'submitForm').andCallFake(fakeFunction);
    });

    describe('if the message is empty', function () {
        beforeEach(function () {
            this.view.$el.find('#message').text('');
            this.view.$el.find('form').submit();
        });
        it('backendController.submitForm and fail Deferred Object should be called', function () {
            expect(submitFormSpy).toHaveBeenCalled();
            // how should I test that fail Deferred Object is called?
        });
    });

    describe('if the message is not empty', function () {
        beforeEach(function () {
            this.view.$el.find('#message').text('some text');
            this.view.$el.find('form').submit();
        });
        it('backendController.submitForm should be called and the fail Deferred Object should be not called', function () {
            expect(submitFormSpy).toHaveBeenCalled();
            // how should I test that fail Deferred Object is not called?
        });
    });

});

推薦答案

我們實際上遇到了同樣的問題,試圖測試代表 AJAXed 模板腳本的延遲對象以進行動態模板.我們的測試解決方案包括將 Jasmine-Ajax 庫與 Jasmine 本身結合使用.

We actually ran into the same problem, trying to test Deferred objects that represent AJAXed template scripts for on-the-fly templating. Our testing solution involves using the Jasmine-Ajax library in conjunction with Jasmine itself.

所以可能會是這樣的:

describe('When Submit button handler fired', function () {
  jasmine.Ajax.useMock();

  describe('if the message is empty', function () {

    beforeEach(function() {
      spyOn(backendController, 'submitForm').andCallThrough();
      // replace with wherever your callbacks are defined
      spyOn(this, 'onSuccess');
      spyOn(this, 'onFailure');
      this.view.$el.find('#message').text('');
      this.view.$el.find('form').submit();
    });

    it('backendController.submitForm and fail Deferred Object should be called', function () {
      expect(backendController.submitForm).toHaveBeenCalledWith('');
      mostRecentAjaxRequest().response({
        status: 500, // or whatever response code you want
        responseText: ''
      });

      expect( this.onSuccess ).not.toHaveBeenCalled();
      expect( this.onFailure ).toHaveBeenCalled();
    });
});

另一件事,如果可以的話,請嘗試分解功能,這樣您就不會在一次測試中測試整個 DOM-to-response-callback 路徑.如果您足夠細化,您實際上可以通過在測試中使用 Deferred 對象本身來測試異步 Deferred 解決方案!

Another thing, if you can, try to break up the functionality so you're not testing the entire DOM-to-response-callback path in one test. If you're granular enough, you can actually test asynchronous Deferred resolutions by using Deferred objects themselves inside your tests!

關鍵是在你的測試中實際使用 Deferred 對象,這樣 expect 調用的范圍仍然在你的 it 功能塊內.

The key is to actually use Deferred objects within your tests themselves, so that the scope of the expect call is still within your it function block.

describe('loadTemplate', function() {
  it('passes back the response text', function() {
    jasmine.Ajax.mock();
    loadTemplate('template-request').done(function(response) {
      expect(response).toBe('foobar');
    });
    mostRecentAjaxRequest().response({ status:200, responseText:'foobar' });
  });
});

這篇關于如何使用 jasmine 測試完成和失敗的延遲對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 成人av影院 | 黑人精品 | 亚洲成人自拍 | 天天天天天天天干 | 日韩在线观看视频一区 | 一区二区三区四区在线 | 91免费入口| 在线免费观看欧美 | 精品成人一区 | 国产女人叫床高潮大片免费 | 国产精品久久国产精品 | 久久精品亚洲精品国产欧美 | 国产福利在线播放麻豆 | 日韩av啪啪网站大全免费观看 | 伊人网在线播放 | 中文字幕免费 | 色综合99| 亚洲精品久久久一区二区三区 | 国产精品久久影院 | 中文字幕一区二区三区四区五区 | 美国黄色毛片 | 国产真实精品久久二三区 | 天天综合干 | 日韩午夜精品 | 九九九国产 | 青青久在线视频 | 国产免费福利小视频 | 九色视频网站 | 久久久久久久国产精品影院 | 亚洲精久 | 美女在线一区二区 | 欧美一级在线视频 | 精品国产色 | www.久久99 | 久久久久久久一区 | 一区中文字幕 | 365夜爽爽欧美性午夜免费视频 | 亚洲一区精品在线 | 亚洲视频区 | 美日韩一区二区 | 免费亚洲婷婷 |