TypeError: undefined is not a function on init function in Angular factory

1,652 views
Skip to first unread message

fpd...@gmail.com

unread,
Aug 4, 2017, 11:50:10 AM8/4/17
to Jasmine
I have simple factory in AngularJS:

function usersListDataProviderFactory(UserResource, $q) {
var usersListDataProvider = {},
queryParams = {};

init(queryParams);

return {
initDataProvider: function (queryParams) {
init(queryParams);
},
getDataProviderPromise: function () {
return usersListDataProvider;
}
};

function init(queryParams) {
var defer = $q.defer();

UserResource.getUsers(queryParams).then(function (response) {
defer.resolve(response);
}, function (error) {
defer.reject(error);
});

usersListDataProvider = defer.promise;
}
}

I have written tests in Karma / Jasmine that pass after commenting the lines:

init(queryParams);

When I restore an automatic function call I get a message:

TypeError: undefined is not a function (evaluating 'UserResource.getUsers(queryParams).then')

TypeError: undefined is not an object (evaluating 'deferred.promise')

TypeError: undefined is not an object (evaluating 'usersListDataProvider.initDataProvider')


I know the problem is the configuration of the test and the moment the spy was created, but I have no idea how to solve the problem. Current test configuration:

describe('Service: UsersListDataProvider', function () {
var usersListDataProvider,
userResourceStub,
deferred,
$rootScope,
$q;

beforeEach(function () {
module('UsersList');
});

beforeEach(function () {
userResourceStub = {
getUsers: function (queryParams) {
return queryParams;
}
};

module(function ($provide) {
$provide.value('UserResource', userResourceStub);
});
});

beforeEach(inject(function (_UsersListDataProvider_, _$rootScope_, _$q_) {
usersListDataProvider = _UsersListDataProvider_;
$rootScope = _$rootScope_;
$q = _$q_;

deferred = _$q_.defer();
}));

beforeEach(function () {
spyOn(userResourceStub, 'getUsers').and.returnValue(deferred.promise);
});

describe('Method: initDataProvider', function () {
it('');
});
});

Any idea ?

fpd...@gmail.com

unread,
Aug 4, 2017, 11:50:10 AM8/4/17
to Jasmine

Gregg Van Hove

unread,
Aug 4, 2017, 1:23:21 PM8/4/17
to jasmi...@googlegroups.com
My best guess is that because your default implementation of `userResourceStub` in the spec doesn't return a Promise, when `init` gets called inline in the outer factory wrapper, the `spyOn` call hasn't been made so you're not getting a deferred out. It's possible that some of the other issues then stem from that initial problem.

Hope this helps. Thanks for using Jasmine!

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

fpd...@gmail.com

unread,
Aug 9, 2017, 4:08:41 AM8/9/17
to Jasmine
Thanks,

I change userResourceStub to:

userResourceStub = {
getUsers: function (queryParams) {
        return {
then: function () {
return queryParams
}
};
}
};

and it works :) 
Reply all
Reply to author
Forward
0 new messages