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

使用 Jasmine 在 JavaScript 中存根 WebSocket

Stubbing WebSocket in JavaScript with Jasmine(使用 Jasmine 在 JavaScript 中存根 WebSocket)
本文介紹了使用 Jasmine 在 JavaScript 中存根 WebSocket的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我嘗試測試 onmessage 是否是一個正確的函數.

I try to test if onmessage is a proper function.

這是一個測試:

  describe(".init(address, window)", function() {
    beforeEach(function() {
      address = 'ws://test.address';
      window = {};
      e = {
        data: {}
      }
      spyOn(window, 'WebSocket').and.returnValue(function() {return {onmessage: null}});
      spyOn(subject, 'handleMessage');
    });

    it("should create a WebSocket client which connects to the given address", function() {
      subject.init(address, window);
      expect(window.WebSocket).toHaveBeenCalledWith(address);
    });

    it("should have onmessage method overriden with a function which handles message", function() {
      ws = subject.init(address, window);
      alert(JSON.stringify(ws));
      ws.onmessage(e);
      expect(subject.handleMessage).toHaveBeenCalledWith(e.data);
    });
  });

這里是實現:

FL.init = function(address, window) {
  if ('WebSocket' in window) {
    var ws = new WebSocket(address);
    ws.onmessage = function(e) {
      this.handleMessage(e.data);
    };
    return ws;
  }
};

第一個測試通過.第二個,wsundefined.這是為什么?我在控制臺中嘗試過 new function() {return {onmessage: null}} 看起來應該沒問題.

The first test passes. In the second, ws is undefined. Why is that? I tried in a console new function() {return {onmessage: null}} and it looks it should be ok.

推薦答案

我不完全確定你的代碼應該如何表現,但如果你需要監視 WebSocket構造函數和存根 .send 方法來模擬一些傳入的消息,這里是如何實現的.

I'm not completely sure that I figured out how your code is supposed to behave, but if you need to spy on WebSocket constructor and stub .send method to mock some incoming messages, here is how to achieve it.

要監視 WebSocket,您需要調用 .and.callThrough 而不是 returnValue.然而,它導致抱怨缺少 new 關鍵字(如 here),所以你需要偽造構造函數:

To spy on WebSocket you will need to call .and.callThrough rather than returnValue. However it result with complains about lack of new keyword (as mentioned in here), so you need to fake the constructor:

var realWS = WebSocket;
var WebSocketSpy = spyOn(window, "WebSocket").and.callFake(function(url,protocols){
  return new realWS(url,protocols);
});

要為傳入的消息做一個間諜,你可以簡單地做

To make a spy for incoming messages you can simply do

var onmessageCallbackSpy = jasmine.createSpy('onmessageCallback');

您還可以監視 .send 方法,并提供一些模擬響應:

You can also spy on .send method, and provide some mocked responses with:

var sendSpy = spyOn(WebSocket.prototype, "send").and.callFake(function(outMsg){
  // process/check outgoing message
  // setTimeout, or call it immediately
  this.onmessage("mock message goes here");
}); 

但請確保使用 WebSocket.prototype,然后再將其替換為 WebSocketSpy.

But make sure to use WebSocket.prototype, before you replace it with WebSocketSpy.

完整的工作示例應該如下所示:

So full working example, should look like this:

it("should spy and callFake WebSocket constructor, and stub prototype methods", function (done) {
    var realWS= WebSocket;  
    var sendSpy = spyOn(WebSocket.prototype, "send").and.callFake(function(outMsg){
      if(outMsg == "outgoing message"){
        this.onmessage("incoming mocked message goes here");
      }
    });  
    // var messageSpy = spyOn(WebSocket.prototype, "onmessage");//.and.returnValue("mock message goes here");      
    var WSSpy = spyOn(window, "WebSocket").and.callFake(function(url,protocols){
      return new realWS(url,protocols);
    }); 
    var onmessageCallbackSpy = jasmine.createSpy('onmessageCallback');       

    // Your code
    // (function init(url, onmessageCallbackSpy){
        var ws = new WebSocket("ws://some/where");
        ws.onmessage = onmessageCallbackSpy;
        // code that results with receiving a message
        // or mocked send, that calls `.onmessage` immediately
        ws.send("outgoing message");
    // })();    

    expect(WSSpy).toHaveBeenCalledWith("ws://some/where");
    expect(onmessageCallbackSpy).toHaveBeenCalledWith("mock message goes here");
    done();
});

這篇關于使用 Jasmine 在 JavaScript 中存根 WebSocket的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 一区二区中文字幕 | 午夜视频在线免费观看 | 狠狠干夜夜草 | 国产在线视频一区二区董小宛性色 | 亚洲一区欧美 | 久久国产一区二区 | 欧美成ee人免费视频 | 成人小视频在线免费观看 | 日韩精品一区二区三区在线观看 | 久久91 | a中文在线视频 | 中文字幕成人 | 欧美成人hd | 一区二区三区四区在线免费观看 | 国产精品福利视频 | 欧美日韩久久精品 | 国产日韩精品一区二区三区 | 欧美久久久久久久久中文字幕 | 亚洲综合激情 | 美女爽到呻吟久久久久 | 久久9久| a级大片免费观看 | 日韩精品一区二区三区中文在线 | 毛片免费观看视频 | 荷兰欧美一级毛片 | 欧美最猛黑人xxxx黑人 | 激情五月激情综合网 | 一区二区三区在线播放 | av网站免费在线观看 | 免费在线一区二区三区 | 青青草这里只有精品 | 成人网视频 | 性福视频在线观看 | 亚洲国产一区二区三区在线观看 | 日韩精品免费一区 | 欧美久久一区 | 成人网在线观看 | 久久久久久一区 | 福利精品 | 国产精品一区久久久 | 麻豆精品国产免费 |