Dallin Dallin - 4 months ago 27
AngularJS Question

How to configure Angular Provider in unit test?

I'm trying to create tests for a provider, but I can't seem to configure it in the test. Here's my provider with which I'm configuring a service with a url when it gets instantiated:

angular.module('PRXHttpData', [])
.provider('HttpData', function(){

var url;

this.setUrl = function(_url_){
url = _url_;
};

this.$get = function(){
return new HttpDataService(url);
}

});

function HttpDataService(url){
this.url = url;
}


Here's my test setup:

beforeEach(function () {
angular.mock.module('PRXHttpData');
});

var HttpDataProvider;

beforeEach(inject(function (_HttpData_) {

HttpDataProvider = _HttpData_;

}));


I tried doing:

beforeEach(function () {
angular.mock.module('PRXHttpData').config(function(HttpData){
HttpData.setUrl('test/url');
});
});


But it gives me the error "Cannot read property 'config' of undefined"

How can I configure my provider in my test?

Answer

angular.mock.module() doesn't return an Angular module and can't be chained.

As said in the manual, it accepts functions as arguments:

any number of modules which are represented as string aliases or as anonymous module initialization functions.

These functions are invoked during config phase. It should be

angular.mock.module('PRXHttpData', function(HttpDataProvider){ ... });