0
votes

I'm trying to unit test a controller in AngularJS using Jasmine. The controller has slightly different syntax than most documentation I can find and I'm getting this error... http://errors.angularjs.org/1.2.15/$injector/unpr?p0=propertiesProvider%20%3C-%20properties

The controller...

myApp.controller('myController', function($scope, $rootScope, param) {

    param.info.get().then(function(example){
        //...
    });

});

The test js...

describe('myApp', function() {
    var $scope, $rootScope, param, createController, properties;
    beforeEach(module('myApp'));

    debugger; //runs

    beforeEach(inject(function($injector) {

        debugger; //doesn't run

        $rootScope = $injector.get('$rootScope');
        properties = $injector.get('properties');
        param = $injector.get('param');
        $scope = $rootScope.$new();
        var $controller = $injector.get('$controller');

        createController = function() {
            return $controller('myController', {
                '$scope': $scope,
                '$rootScope':$rootScope,
                'properties':properties,
                'param':param,
            });
        };
    }));

    var a;

    it('should pass this basic test no matter what', function() {
        a = true;

        expect(a).toBe(true);
    });
});

Param definition...

myApp.factory("param", function($http) {
return {
    info: {
        get: function() {
            return $http.get(myApp.url("/param/info")).then(function(response) {
                return response.data.data;
            });
        }
    }
}

myApp.run...

myApp.run(['$http', '$rootScope', 'properties', function($http, $rootScope, properties){
...
}]);
1
How does your param definition look like ? Where in your code are you injecting the properties service ?gkalpak
make sure the file containing the param service is loaded by karmaJonathan de M.
@ExpertSystem I added the param definition. I don't think I'm injecting properties anywhere, can you explain that?j_buckley
@j_buckley: According to the error you are getting, there seems to be a properties injected somewhere, and the error is sue to Angular not being able to find an implementation for the properties service. Search your code-base for "properties".gkalpak
@ExpertSystem Ok I see what you mean. I updated the code with the properties injection. However, I added two debuggers but the first one runs and the second one doesn't. Do you see a problem with beforeEach(inject(function($injector) {? I have also included myApp.run where it looks like properties comes from.j_buckley

1 Answers

0
votes

If you look at the error carefully it says error in propertiesProvider, you are injecting properties in your test , and hence it's looking for propertiesProvider which doesn't exist . so it throws error.

If properties is an angular Service and you are injecting that in your controller , while testing you need not inject that service again to your test, angular mock takes care of that .

I'll suggest you use npm package generator-yosapy to bootstrap your controller test