更新angular-route 1.5.0到1.5.1时的Karma错误

Jér*_*lle 9 javascript unit-testing angularjs karma-jasmine

当我将angular-route 1.5.0更新为1.5.1时,我有一个错误:

角度单位测试:错误:意外请求:GET

当我启动业力时,我有一条错误消息:

1)调用getAll方法[应用程序类别]错误:意外请求:获取http://myapp.com/app-category?is_active=true /node_modules/angular-mocks/angular-mocks.js中不再需要更多请求

app_category.model.test.js

describe('[App Category]', function () {

    beforeEach(module('myApp'));

    var $httpBackend, HttpService, AppCategory;

    beforeEach(inject(function (_$httpBackend_, _HttpService_, _AppCategory_) {
        $httpBackend = _$httpBackend_;
        HttpService  = _HttpService_;

        AppCategory = _AppCategory_;
    }));

    it('Call getAll method', function () {
        var app_category = new AppCategory();

        HttpService.mock('GET', 'app-category?is_active=true', 200, [{ code: 'TRAVEL', name: 'Travel' }]);

        app_category.getAll({ is_active: true }).then(function (request) {
            expect(request.data[0].code).toBe('TRAVEL');
            expect(request.data[0].name).toBe('Travel');
        });

        $httpBackend.flush();
    });

});
Run Code Online (Sandbox Code Playgroud)

角mockHTTP.js

(function (angular) {
    'use strict';

    angular.module('ngMockHTTP', []).service('HttpService', function ($httpBackend, config) {
        this.endpoint = config.api.endpoint;

        this.mock = function (method, url, status, response) {
            switch (method) {
                case 'GET':
                    $httpBackend
                        .expectGET(this.endpoint + url)
                        .respond(status, response);
                break;
                case 'POST':
                    $httpBackend
                        .expectPOST(this.endpoint + url)
                        .respond(status, response);
                break;
                case 'PUT':
                    $httpBackend
                        .expectPUT(this.endpoint + url)
                        .respond(status, response);
                break;
                case 'PATCH':
                    $httpBackend
                        .expectPATCH(this.endpoint + url)
                        .respond(status, response);
                break;
                case 'DELETE':
                    $httpBackend
                        .expectDELETE(this.endpoint + url)
                        .respond(status, response);
                break;
            }
        };
    });
})(angular);
Run Code Online (Sandbox Code Playgroud)

我试图重新说明:

case 'GET':
    $httpBackend
        .expectGET(this.endpoint + url)
        .respond(status, response);
break;
Run Code Online (Sandbox Code Playgroud)

这样 :

case 'GET':
    $httpBackend
        .whenGET(this.endpoint + url, {
    }).respond(function(){ return [200, response]});
break;
Run Code Online (Sandbox Code Playgroud)

但我有同样的错误

我用茉莉2.4.1,angularjs 1.5.3,业力0.13.0

Jér*_*lle 1

我更新了 angularjs 1.5.5,它可以工作了!

来自 CHANGELOG.md 1.5.5(2016-04-18 发布):

“ngRoute:允许 ngView 包含在异步加载的模板中 急切加载 $route,可能会破坏测试,因为它可能会请求根或默认路由模板($httpBackend 对此一无所知)。

它将重新应用于 v1.6.x,并提供重大更改通知,并可能提供在测试中禁用该功能的方法。”