2015-03-31 209 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(); 
相关问题