2015-03-31 210 views
4

我有一堆被順利單元測試,我已經開始量角器E2E測試添加到我的項目。我正在測試頁面上的交互式元素,但我無法測試從瀏覽器發出的某些數據。

舉例來說,我想看看如果點擊某個按鈕產生POST一定的端點。

我有量角器設置使用如下:

/*globals global*/ 
module.exports = function() { 
    'use strict'; 
    var chai = require('chai') 
    , promised = require('chai-as-promised'); 

    global.expect = chai.expect; 

    chai.use(promised); 
}(); 

我知道如何使用量角器互動:

it('send data to the "log" endpoint when clicked', function() { 
    var repeater = element.all(by.repeater('finding in data.items')); 

    repeater.get(0).click().then(function() { 
     // $http expectation 
    }); 
}); 

不過,我不知道如何設置$httpBackend在量角器所以我可以捕獲由於.click()事件而發送的數據。我需要額外的模塊嗎?

在噶/摩卡我只想:

beforeEach(module('exampleApp')); 

describe('logging service', function() { 
    var $httpPostSpy, LoggingService; 

    beforeEach(inject(function(_logging_, $http, $httpBackend) { 
     $httpPostSpy = sinon.spy($http, 'post'); 
     LoggingService = _logging_; 
     backend = $httpBackend; 
     backend.when('POST', '/api/log').respond(200); 
    })); 

    it('should send data to $http.post', function() [ 
     LoggingService.sendLog({ message: 'logged!'}); 
     backend.flush(); 
     expect($httpPostSpy.args[0][1]).to.have.property('message'); 
    }); 
}); 

但我不知道如何讓一個參考量角器$httpBackendinject模塊。

回答

1

端到端測試是有關測試代碼是方式是類似於最終用戶將如何做。因此,驗證遠程請求是否應該根據可見結果進行驗證,例如將數據加載到div或網格中。

不過,如果你想驗證遠程請求製成,您可以創建使用ngMockE2E模塊,它包含一個模擬$htpBackend一個類似於在ngMock一個模擬的後端設置。

查看文檔的$httpBackendhttps://docs.angularjs.org/api/ngMockE2E/service/ $ httpBackend

0

$ httpBackend是嘲諷到服務器的虛擬來電。在e2e測試中,你通常確實想要實際上打電話給服務器。重要的是要注意,量角器中的大多數元素定位器返回promises

與此代碼的測試就知道要等到來自服務器的響應返回,然後斷言文字是p標籤是指從服務器的正確數據。

我-file.spec.js

'use strict'; 

describe('The main view', function() { 
    var page; 

    beforeEach(function() { 
    browser.get('/index.html'); 
    page = require('./main.po'); 
    }); 

    it('should have resultText defined', function() { 
     expect(page.resultText).toBeDefined() 
    }) 


    it('should display some text', function() { 
    expect(page.resultText.getText() 
     .then()) 
     .toEqual("data-that-should-be-returned"); 
    }); 


}); 

我-file.po.js

'use strict'; 

var MainPage = function() { 
    this.resultText = element(by.css('.result-text')); 


}; 

module.exports = new MainPage(); 
相關問題