Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown provider: $provideProvider angularjs jasmine

I am getting this error: "Error: [$injector:unpr] Unknown provider: $provideProvider <- $provide". I been stuck for hours googling around. I have seen many examples where it is done this way and I am unsure of what to do.

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

});

like image 400
user2170878 Avatar asked Jan 03 '15 01:01

user2170878


2 Answers

You had a few bonus provides. In particular the one in the inject statement. You can't inject provide, it is only available to modules. Try my changes below.

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

});

Have a read about the angularjs concept of providers, and also check your code against this guide:

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

like image 175
Peter Ashwell Avatar answered Nov 17 '22 10:11

Peter Ashwell


$provide is a provider, you can only inject $provide in app.config method, not in controller method.

like image 41
karthik pamidimarri Avatar answered Nov 17 '22 09:11

karthik pamidimarri