faebser
faebser

Reputation: 149

Unit testing a service in angularJS

I'm relatively new to unit testing but was now tasked to write tests for a existing code base that I know quite well.

Sadly I'm unable to achieve any progress what so ever nor find really helpful documentation.

The main component of the code base is a service to retrieve data from an api but I'm unable to get an instance of the module the service belongs to:

TypeError: module is not a function in /home/faebser/workspace/GridSense-CMS-App/dev/test/unit/api.test.js (line 13)

karma config: http://sprunge.us/ObSP?js

tests: http://sprunge.us/AJWL?js

karma output: http://sprunge.us/WYHI?bash

What is the problem? why am I not able to get a instance of the module?

UPDATE1:

I'm managed to load my module by reinstalling the same version of angular and angular mocks. But now I run into the following error:

minErr/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:63:12
loadModules/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4138:15
forEach@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:323:11
loadModules@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4099:5
createInjector@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4025:11
workFn@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular-mocks/angular-mocks.js:2425:44

I managed to track the error down to the following:

"[$injector:modulerr] Failed to instantiate module ui.router due to:[$injector:nomod] Module 'ui.router' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.http://errors.angularjs.org/1.3.15/$injector/nomod?p0=ui.routerminErr/<@http://localhost:9876/base/bower_components/angular/angular.js:63:12module/<@http://localhost:9876/base/bower_components/angular/angular.js:1774:1ensure@http://localhost:9876/base/bower_components/angular/angular.js:1698:38module@http://localhost:9876/base/bower_components/angular/angular.js:1772:1loadModules/<@http://localhost:9876/base/bower_components/angular/angular.js:4115:22forEach@http://localhost:9876/base/bower_components/angular/angular.js:323:11loadModules@http://localhost:9876/base/bower_components/angular/angular.js:4099:5loadModules/<@http://localhost:9876/base/bower_components/angular/angular.js:4116:40forEach@http://localho"

Okay, simply forgot to add ui-router to karma-config.

Upvotes: 2

Views: 1055

Answers (2)

faebser
faebser

Reputation: 149

Okay, I managed to get this to work. The main problem was the strange error messages that angular gave me:

minErr/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:63:12
loadModules/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4138:15
forEach@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:323:11
loadModules@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4099:5
createInjector@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4025:11
workFn@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular-mocks/angular-mocks.js:2425:44

To work around this I used the karma debug window and set a breakpoint at angular.js:63 in function minErr(module, ErrorConstructor):

return new ErrorConstructor(message);

that way you can see the arguments and the compiled error message. In the end i just forgot to add a few dependencies in the karma config file.

Upvotes: 0

Florian Orpeliere
Florian Orpeliere

Reputation: 184

It's looks like angular-mock is not loaded.

Checks well your path. For the moment, for karma it's look like :

-dev |-test/ |-js/ |-bower_components/ |-karma.conf

Because in karma.conf, you have :

base: '',

Upvotes: 2

Related Questions