AngularJS与$resource和$httpBackend的怪异行为

AngularJS与$resource和$httpBackend的怪异行为,angularjs,jasmine,angular-resource,Angularjs,Jasmine,Angular Resource,我将以下AngularJS资源作为服务公开 myServices.factory('Service1', ['$resource', function ($resource) { var Registrations = $resource('/api/v1/registration/:id'); return { getForTcn: function(tcn) { return Registr

我将以下AngularJS资源作为服务公开

 myServices.factory('Service1', ['$resource',
    function ($resource) {

        var Registrations = $resource('/api/v1/registration/:id');

        return {
            getForTcn: function(tcn) {
                return Registrations.query({ tcn: tcn, state: 'Active' }).$promise;
            }

        };
    } ]);
我有一个茉莉花测试,失败如下

beforeEach(inject(function (_$httpBackend_, Service1) {
    $httpBackend = _$httpBackend_;
    service = Service1;

    $httpBackend.when('GET', /^\/api\/v1\/registration\?.*/).respond([]);
}));afterEach(function () {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
});
在上面的设置中,下面的断言失败

it('should have correct http GET request', function () {
    Service.getForTcn('1234');
    $httpBackend.expectGET('/api/v1/registration?tcn=1234&State=Active');
    $httpBackend.flush();
});
如果我交换服务函数和断言中参数的位置,测试就会通过

getForTcn: function(tcn) {
                return Registrations.query({ state: 'Active', tcn: tcn }).$promise;
            }
$httpBackend.expectGET('/api/v1/registration?state=Active&tcn=1234');
知道这是怎么回事吗? 我还没有尝试在测试之外执行失败的版本是否真的有效,但有人看到过类似的行为吗?

谢谢

问题在于查询参数是按字母顺序排列的,因此如果您将
foo
作为参数,那么您的断言将是:

$httpBackend.expectGET('/api/v1/registration?foo=1234&state=Active');
$httpBackend.expectGET('/api/v1/registration?state=Active&zoo=1234');
这一切都会过去。相反,如果参数为
zoo
,则断言为:

$httpBackend.expectGET('/api/v1/registration?foo=1234&state=Active');
$httpBackend.expectGET('/api/v1/registration?state=Active&zoo=1234');

这就是为什么您必须将
tcn
放在
state
参数之后,正如您在问题中指出的那样。

是否有方法防止AngularJS对参数进行排序,以便在将参数传递到操作时保留参数顺序?