在量角器访问$ HTTP数据/端到端测试(AngularJS)

编程入门 行业动态 更新时间:2024-10-10 18:22:59
本文介绍了在量角器访问$ HTTP数据/端到端测试(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一堆被顺利单​​元测试,我已经开始量角器端到端测试,添加到我的项目。我做的还好在页面上测试的互动元素,但我无法发送出去的浏览器测试某些数据。

I have a bunch of Unit tests that are going well, and I've started to add Protractor E2E tests to my project. I'm doing okay testing interactive elements on the page, but I'm having trouble testing for certain data being sent out of the browser.

比如说,我想看看,如果单击某个按钮生成一个 POST 一定端点。

For instance, I want to see if clicking a certain button produces a POST to a certain endpoint.

我有量角器设置使用下列内容:

I have protractor set up using the following:

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

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

I understand how to use Protractor to interact:

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 在量角器,所以我可以捕获被作为。点击()事件。我是否需要额外的模块?

However, I don't know how to set up $httpBackend in Protractor so I can capture the data that gets sent as a result of the .click() event. Do I need an additional module?

在噶/摩卡我就简单:

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 和注模块的参考。

推荐答案

端到端测试是有关测试code是方式类似于最终用户会怎么做。所以验证远程请求是否是由应针对可见结果进行验证,如数据得到装入格或栅格。

End to end testing is about testing the code is manner that is similar to how an end user will do. So verifying whether a remote request is made should be validated against a visible outcome, such as data getting loaded into a div or grid.

不过,如果你想验证的远程请求,您就可以创建一个使用 ngMockE2E 模块,它包含一个模拟的模拟后端设置 $ htpBackend 一个类似于在 ngMock 。

Still if you want to validate remote requests are made, you can create a mock back end setup using the ngMockE2E module, which contains a mock $htpBackend similar to the one in ngMock.

只看了 $ httpBackend 的 docs.angularjs/api/ngMockE2E/service/ $ httpBackend

Look at the documentation on the $httpBackend docs.angularjs/api/ngMockE2E/service/$httpBackend

更多推荐

在量角器访问$ HTTP数据/端到端测试(AngularJS)

本文发布于:2023-11-25 09:13:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:量角器   端到   测试   数据   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!