如何使用 karma 和 mocha+chai 对 angularjs 路由的 resolve 进行单元测试?

How to unit test angularjs route's resolve with karma and mocha+chai?

我正在开发一个需要在路由器 (ngRoute) 中解决承诺的应用程序。问题是我不确定如何为此编写单元测试,我正在使用 karma 和 mocha 和 chai。

这是我要测试的代码部分:

function config ($routeProvider) {
    $routeProvider
        .when('/', {
             templateUrl: 'views/orders.html',
             controller: 'OrderController',
             controllerAs: 'vmr',
             resolve: OrderController.resolve,
             data: {...}
    });
}

function OrderController (OrderService, newOrders) {
    this.newOrders = newOrders;
}

OrderController.resolve = {
    newOrders: function (OrderService) {
        return OrderService.getOrders();
    }
};

这就是我在还没有解析部分时开始编写单元测试的方式:

describe('OrderController', function() {

    'use strict';

    var controller,
        service,
        httpBackend;

    beforeEach(module('myApp.orders'));

    beforeEach(inject(function($controller, _OrderService_, $httpBackend) {
        service = _OrderService_;
        httpBackend = $httpBackend;
        // Create the controller
        controller = $controller('OrderController', {});
    }));

    beforeEach(function() {
        httpBackend.when('GET', 'url/to/get/orders')
            .respond(200, {[...]});
    });

    afterEach(function() {
        httpBackend.verifyNoOutstandingExpectation();
        httpBackend.verifyNoOutstandingRequest();
    });

    it('should get the list of new orders', function() {
        httpBackend.flush();
        expect(controller.neworders).not.to.undefined;
        expect(controller.neworders.length).to.equal(3);
    });
});

此时出现错误:

Unknown provider: newOrdersProvider <- newOrders

我明白为什么会出现这个错误,但我不知道如何解决。基本上我不知道如何测试在路由中解析的承诺。

在此先感谢您的帮助!

经过大量搜索和阅读AngularJS Testing Cookbook,我发现了如何在控制器中注入 promise 的结果。 主要代码没有改变,所以我将 post 这里只更新单元测试的代码:

describe('OrderController', function() {

    'use strict';

    var controller,
        service,
        httpBackend;

    // here is where I will inject a new value
    beforeEach(function() {
        module('myApp.orders', function($provide) {
            $provide.value('resolver', {
                newOrders: function(service) {
                    return service.getOrders();
                }
            });
        });
    });

    beforeEach(inject(function($controller, _OrderService_, $httpBackend, resolver) {
        service = _OrderService_;
        httpBackend = $httpBackend;
        // Create the controller
        controller = $controller('OrderController', {
            // add them to the controller
            newOrders: resolver.newOrders(service)
        });
    }));

    beforeEach(function() {
        httpBackend.when('GET', 'url/to/get/orders')
            .respond(200, {[...]});
    });

    afterEach(function() {
        httpBackend.verifyNoOutstandingExpectation();
        httpBackend.verifyNoOutstandingRequest();
    });

    it('should get the list of new orders', function() {
        httpBackend.flush();
        expect(controller.neworders).not.to.undefined;
        expect(controller.neworders.length).to.equal(3);
    });
});

如果有人有 better/different 解决方案,我也想听听!