Dallin Dallin - 1 year ago 66
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 () {

var HttpDataProvider;

beforeEach(inject(function (_HttpData_) {

HttpDataProvider = _HttpData_;


I tried doing:

beforeEach(function () {

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

How can I configure my provider in my test?


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