Angular $ httpBackend.expectGET - 响应409状态&自定义statusText [英] Angular $httpBackend.expectGET - respond with 409 status & custom statusText
问题描述
尝试在单元测试中创建$ httpBackend.expectGET以返回状态409和自定义statusTextTestPhrase。
Trying to create a $httpBackend.expectGET in a unit test to return a status of 409 and a custom statusText of 'TestPhrase'.
查看Angular文档( https://docs.angularjs.org/api/ngMock/service/$httpBackend )给出了以下返回内容的示例:
Looking at the Angular docs (https://docs.angularjs.org/api/ngMock/service/$httpBackend) it gives the following example of what is returned:
{function([status,] data [,headers,statusText])|
函数(函数(方法,url,数据,标题)}
{function([status,] data[, headers, statusText]) | function(function(method, url, data, headers)}
无法理解如何解释上面的例子。
Having trouble understanding how to interpret the above example.
我目前的代码是:
$httpBackend
.expectGET('/example/url')
.respond(function () {
return ['409', ['TestPhrase'], {}];
});
我正在测试的代码是:
$http.get('/example/url').then(function (response) {
console.log('success, status ' + response.status);
console.log('success, statusText ' + response.statusText);
}, function(response) {
console.log('error, status ' + response.status);
console.log('error, statusText ' + response.statusText);
}
});
我从此测试中收到的控制台输出是:
The output to console I am receiving from this test is:
'error, status 409'
'error, statusText '
预期输出为:
'error, status 409'
'error, statusText TestPhrase'
推荐答案
文档说:
回复 -
{function([status,] data [ ,headers,statusText])|函数(函数(方法,url,数据,头文件)}
- response方法接受一组要返回的静态数据或一个可以返回包含响应状态(数字),响应数据的数组的函数(字符串),响应头(Object)和状态文本(字符串)。
respond –
{function([status,] data[, headers, statusText]) | function(function(method, url, data, headers)}
– 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).
你正在做第二个选项传递一个返回数组的函数。这是应该工作的。状态文本是第4项。(为清楚起见,我包括可选的函数参数。)
You're doing the second option, passing a function that returns an array. Here's what should work. Status text is the 4th item. (For clarity, I included the optional function parameters.)
$httpBackend
.expectGET('/example/url')
.respond(function (method, url, data, headers) {
return [409, 'response body', {}, 'TestPhrase'];
});
这篇关于Angular $ httpBackend.expectGET - 响应409状态&自定义statusText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!