Unit Test Controller: Injected Service is empty or undefined

566 views
Skip to first unread message

Olivier Clément

unread,
Jun 26, 2013, 7:50:37 PM6/26/13
to ang...@googlegroups.com
Hey Folks,

So I am at the early stages of an Proof of Concept I'm working on, and trying to get unit testing right, I want to test a very simple controller that relies on a service I wrote.

Right now, all it does is the controller calls a method on the service, that returns an array to the ctrl.

I can test whatever is in the scope of the controller, which is fine. But I cannot seem to access anything related to the service I (try to) inject in there. It appears empty, as if it were an empty object.

I inlined comments to show what happens.

Feel free to clone and run 'grunt test' if you want to fiddle with this.

Thanks

Ricardo Bin

unread,
Jun 26, 2013, 10:50:24 PM6/26/13
to ang...@googlegroups.com
Hi,

I just have a quick look to your code... Did you try to use spyOn instead of jasmine.spyOn ?

Olivier Clément

unread,
Jun 26, 2013, 11:17:08 PM6/26/13
to ang...@googlegroups.com
Hey there
Thanks for the reply

I just tried spyOn: Looks like that was the answer (or part of it; I'm not sure due to my lack of unit testing experience)

It doesn't throw an error on the spyOn method now.
If I do 'expect obj.method toHaveBeenCalled', it fails on that. But that might be because of the fact that I call that method in the body of the controller directly? -> I mean, maybe the method hasn't been called when the test execute?

 Also, any idea why everywhere I see the use of 'jasmine.spyOn', while only 'spyOn' works for me?

Thank a lot, it's definitely a good step in the right direction though;

Ricardo Bin

unread,
Jun 26, 2013, 11:25:12 PM6/26/13
to ang...@googlegroups.com
Well, I use jasmine for years and always just use spyOn . BTW, even it their doc, the right use is this http://pivotal.github.io/jasmine/  :P

Maybe the expectation fails because articlesServices.getAllArticles dont exists. But $scope.articlesServices.getAllArticles should exists, and its your spy.

Olivier Clément

unread,
Jun 27, 2013, 10:06:20 AM6/27/13
to ang...@googlegroups.com
Again, thanks for your answers, I appreciate the help;

I have a global var 'service' to which I assign 'articlesService' in the 'beforeEach(inject ....)' block;

So technically I should be able to use 'expect(service.getAllArticles).toHaveBeenCalled() no?

Note: I tried spyOn($scope.articlesService, ...) -> spyOn could not find an object to spy upon for getAllArticles()
so I guess this won't ever work;

I can spyOn(service, ....) though




So going with that spy, which seems to do his work, doing:  

expect(service.getAllArticles).toHaveBeenCalled()
throws:
Expected spy getAllArticles to have been called.
Error: Expected spy getAllArticles to have been called.


Maybe the call is done after the assertion?

In the controller:

function ($scope, articlesService) {
var self;
self = this;

$scope.articles = {
list: []
};

$scope.articles.list = articlesService.getAllArticles();
[....]
Reply all
Reply to author
Forward
0 new messages