2016-05-23 20 views
0

我試圖將UnitTest與websocket結合使用Jasmine框架。 (jasmine-2.4.1 ver。)如何在茉莉花框架中使用websocket?

websocket send功能沒問題。但onmessage函數有問題。

結果值未定義。

這裏是我的代碼:

var ws; 
beforeEach(function() { 
    ws = new WebSocket("ws://myaddress:port"); 
}); 

describe("Module Test", function() { 

    it("first test", function (done) { 
     //send to server 
     ws.onopen = function(e) { 
      ws.send(JSON.stringify({"module":"test","func":"test_func"})); 
     }; 

     var result; 
     ws.onmessage = function(e) { 
      result = JSON.parse(e.data.rsp); 
      console.log(result); // <- this result value is true... 
     }; 

     //server return result value.. and... it is true..... 
     //but result value is undefiend.... 
     expect(result).toBe(true); 
     done(); 
    }); 
}); 

回答

0

你希望你收到消息之前的結果是truthy。原諒我,如果我誤解了這個問題,但這應該修復你的測試: -

var ws; 
beforeEach(function() { 
    ws = new WebSocket("ws://myaddress:port"); 
}); 

describe("Module Test", function() { 

    it("first test", function (done) { 
     ws.onopen = function(e) { 
      ws.send(JSON.stringify({"module":"test","func":"test_func"})); 
     }; 

     ws.onmessage = function(e) { 
      var result = JSON.parse(e.data.rsp); 
      expect(result).toBeTruthy(); 
      done(); 
     }; 
    }); 
}); 
+0

var結果值未被滿足。 所以,toBeTruthy()不起作用。 :( –

+0

@SunghoPark道歉...我認爲你的onmessage函數是'真正的',這就是你的評論旁邊的說法,這就是爲什麼我認爲這是在檢索結果值之前斷言的簡單錯誤 – Shakespeare