未知提供者:$ provideProvider angularjs jasmine

时间:2015-01-03 01:51:50

标签: angularjs jasmine

我收到此错误:"错误:[$ injector:unpr]未知提供商:$ provideProvider< - $ provide"。我被困在谷歌上搜索了几个小时。我已经看到了许多以这种方式完成的例子,我不确定该怎么做。

"use strict";


describe('Controller: ProfileCtrl', function ($provide) {
    //load the controller's module
    var mockProfileFactory;
    beforeEach(function() {
        module('profileUpdate', function($provide) {
            mockProfileFactory = {
                get: function() {
                    id: 16
                }
            };
            $provide.value('Profile', mockProfileFactory);
        });
        var ProfileCtrl;
        var scope;

    inject(function ($controller, $rootScope, $provide) {

        scope = $rootScope.$new();
        ProfileCtrl = $controller('ProfileCtrl', {
            $scope: scope
        });
    });
});

it('should have 3 items', function() {
    var things = scope.range(1,3,1);
    expect(things).toBe(3);
});

});

2 个答案:

答案 0 :(得分:17)

你有一些奖金。特别是注入语句中的那个。您无法注入提供,它仅适用于模块。请尝试以下更改。

"use strict";


// SEE no provide here
describe('Controller: ProfileCtrl', function () {
    //load the controller's module
    var mockProfileFactory;
    beforeEach(function() {
        module('profileUpdate', function($provide) {
            mockProfileFactory = {
                get: function() {
                    id: 16
                }
            };
            $provide.value('Profile', mockProfileFactory);
        });
        var ProfileCtrl;
        var scope;
        // SEE and neither in the inject here
    inject(function ($controller, $rootScope) {

        scope = $rootScope.$new();
        ProfileCtrl = $controller('ProfileCtrl', {
            $scope: scope
        });
    });
});

it('should have 3 items', function() {
    var things = scope.range(1,3,1);
    expect(things).toBe(3);
});

});

阅读有关提供者的angularjs概念,并根据本指南检查您的代码:

http://nathanleclaire.com/blog/2013/12/13/how-to-unit-test-controllers-in-angularjs-without-setting-your-hair-on-fire/

答案 1 :(得分:4)

$ provide是一个提供者,你只能在app.config方法中注入$ provide,而不是     控制器方法。