2016-06-28 68 views
0

是否有遠程任何方式來模擬量角器測試中的任何SSE(服務器發送事件)?量角器:嘲諷EventSource

這意味着嘲笑EventSource

角控制器:

angular.module('app').controller('HomeController', function() { 

    var monitoringEvents = new window.EventSource('/streams/jobserveur'); 

    monitoringEvents.addEventListener('monitoring-event', function(e) { 
    var json = JSON.parse(e.data);  
    ... 
    }); 
}); 

謝謝你的任何洞察

+2

你能表現出一定的代碼,其中將需要的嘲諷? (或者你嘗試過的一些代碼並沒有工作?) – acdcjunior

+0

我添加了引用EventSource的控制器代碼。 我對於如何嘲笑這件事情一無所知。也許我可以將'EventSource'封裝到Angular模塊中,並在測試中使用'AddMockModule' ... – nastyklad

回答

0

我管理由我mentionned溶液(角模塊/量角器addMockModule)嘲笑EventSource

  1. 外部化EventSource呼叫到專用角模塊

    angular.module('app.sse', []) 
    .value('$sse', { 
        sources : [], 
        addEventSource : function(name, url) { 
        this.sources[name] = new window.EventSource(url); 
        }, 
        addEventListener : function(name, eventName, callback) { 
        this.sources[name].addEventListener(eventName, callback); 
        } 
    }); 
    
  2. 引用模塊中的應用

    angular.module('app', ['app.sse', ...]) 
    
  3. 使用$sse模塊中的應用

    angular.module('app').controller('HomeController', ['$sse' , function($sse) { 
        $sse.addEventSource('jobserveur', '/streams/jobserveur'); 
    
        $sse.addEventListener('jobserveur', 'monitoring-event', function(e) { 
        var js = JSON.parse(e.data); 
        } 
    }]); 
    

    從這裏,請確保您的應用程序移動到測試

  4. 嘲笑app.sse模塊在測試

    describe('SSE Fixture', function() { 
        beforeEach(function() { 
        browser.addMockModule('app.sse', function() { 
         angular.module('app.sse', []).value('$sse', { 
         addEventSource: function(name, url) { 
    
         }, 
         addEventListener: function(name, event, callback) { 
    
         } 
         }); 
        }); 
    } 
    

    之前還是工作,你就大功告成了!很明顯,這兩種方法在這裏都沒有實現,反正它也是app.sse模塊,但你得到的圖片。

希望它可以幫助任何人

乾杯