Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何单元测试angularjs路线';让我们用因果报应和摩卡咖啡来解决问题+;柴?_Angularjs_Unit Testing_Ngroute - Fatal编程技术网

如何单元测试angularjs路线';让我们用因果报应和摩卡咖啡来解决问题+;柴?

如何单元测试angularjs路线';让我们用因果报应和摩卡咖啡来解决问题+;柴?,angularjs,unit-testing,ngroute,Angularjs,Unit Testing,Ngroute,我正在开发一个应用程序,需要在路由器(ngRoute)中解析承诺。问题是,我不确定如何为此编写单元测试,我正在使用摩卡和柴的karma 下面是我想测试的代码部分: function config ($routeProvider) { $routeProvider .when('/', { templateUrl: 'views/orders.html', controller: 'OrderController',

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

下面是我想测试的代码部分:

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);
    });
});
在这一点上,我得到了错误:


未知提供者:newOrdersProvider经过大量搜索和阅读,我发现如何将promise的结果注入控制器。 主代码没有更改,因此我将在这里仅发布单元测试的更新代码:

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);
    });
});

如果有人有更好/不同的解决方案,我也想听听

谢谢。。我几乎到处找这个我很高兴它对某人有用!