我有一堆运行良好的单元测试,并且已经开始将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
在量角器中进行设置,因此我无法捕获由于.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');
});
});
但是我不知道如何在Protractor中获得对$httpBackend
和inject
模块的引用。
端到端测试是关于测试代码的方式,类似于最终用户的方式。因此,应根据可见结果验证是否发出了远程请求,例如将数据加载到div或网格中。
仍然,如果您想验证是否发出了远程请求,则可以使用ngMockE2E
模块创建一个模拟后端设置,该模块包含$htpBackend
与中类似的模拟ngMock
。
查看$httpBackend
https://docs.angularjs.org/api/ngMockE2E/service/ $ httpBackend上的文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句