gpt4 book ai didi

angularjs - 如何设置 e2e Protractor 后端请求模拟/ stub

转载 作者:行者123 更新时间:2023-12-03 06:56:55 25 4
gpt4 key购买 nike

我尝试设置我的独立 Protractor 项目来模拟我的一些后端请求。因此,我包含了 angular-mocks.js 并在 protractor.conf.js 的 onPrepare() 函数中附加了另一个模块:

browser.addMockModule('httpBackend', function() {
angular.module('httpBackend', ['myApp', 'ngMockE2E']).run(function($httpBackend) {
$httpBackend.whenPOST(/^requests\/*/).respond(function(method, url, data) {
var obj = {"msg": "Response!"};
return [200, JSON.stringify(obj), {}];
});
})
})

这可以让我拦截任何请求,但我没有得到我想要在respond()中返回的内容。看来我只是得到了 200 OK

我做错了什么?

最佳答案

只是为了让你知道我是如何解决这个问题的:

文档说明如下:

The respond method takes a set of static data to be returned or a function that can return an array containing response status (number), response data (string), response headers (Object), and the text for the status (string).

就我而言,标题对象似乎不是可选的,我最终在返回数组之前自行设置它:

browser.addMockModule('httpBackend', function() {
angular.module('httpBackend', ['myApp', 'ngMockE2E']).run(function($httpBackend) {
$httpBackend.whenPOST(/^requests\/*/).respond(function(method, url, data) {
var obj = {"msg": "Response!"},
resHeader = {
"Cache-Control": "no-cache, no-store, max-age=0",
"Date": "Tue, 24 Nov 2015 17:08:57 GMT",
"Pragma": "no-cache",
"Transfer-Encoding": "chunked",
"Content-Type": "application/json; charset=UTF-8",
"Expires": "Thu, 01 Jan 1970 00:00:00 GMT",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "origin,x-requested-with,access-control-request-headers,content-type,access-control-request-method,accept",
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, DELETE",
"Access-Control-Credentials": "true",
"Content-Language": "de-DE",
"Access-Control-Max-Age": "3600"
};
return [200, JSON.stringify(obj), resHeader];
});
})
})

有人知道为什么这是必要的或者它的哪些属性已经过时了吗?

关于angularjs - 如何设置 e2e Protractor 后端请求模拟/ stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33897510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com