我有一堆进展顺利的单元测试,我已经开始将 Protractor 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
在 Protractor 中进行设置,以便捕获因.click()
事件而发送的数据。我需要额外的模块吗?
在 Karma/Mocha 中,我会简单地:
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');
});
});
但我不知道如何在量角器中获得对$httpBackend
和模块的引用。inject