我为我的 angularjs 应用程序编写了一些单元测试。对于测试,我使用内部 $httpBackend 模拟 $http 请求。
在测试期间,我使用 $httpBackend.expectGET,因为我想要应用程序请求的确切行为。
例如我有一个参数对象:
parameters = {
name : 'Monkey',
crazy : false,
desc : 'Nobody',
};
我的应用程序中的 Http-Get 请求是:
return $http.get(this.uri + '/' + id, {params : parameters});
在我的单元测试中,我期望这样:
$httpBackend.expectGET(instance.uri + '/' + returnValues.id + '?' + query).respond(200, object);
“查询”只是对象的元素与“=”和“&”连接。 所以我期望网址:
www.example.com/api/v1/object/1?name=Monkey&crazy=false&desc=Nobody
但我明白了:
www.example.com/api/v1/object/1?crazy=false&desc=Nobody&name=Monkey
$http 是否根据对象中的键对“params”进行排序?
最佳答案
是的,$http
在将参数发送到服务器之前根据 source code 对参数进行排序。
forEachSorted(params, function(value, key) {
...
});
因此,您的测试应该期望有序键,或者您可以通过将其发送到 $http 来编写自己的 paramSerializer提供商:
paramSerializer - {string|function(Object):string} - A function used to prepare string representation of request parameters (specified as an object). Is specified as string, it is interpreted as function registered in with the {$injector}.
关于javascript - $http 是否对 params 对象进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29622610/