Why does my Jasmine spec think my Angular module is undefined

微笑、不失礼 提交于 2019-12-06 06:50:58

This error

TypeError: Cannot read property 'be' of undefined

clearly says that there's something wrong in this piece of code

expect(common).to.be.a('Object');

and not in common service.

This results from the fact that the code was pasted from the spec that uses Chai framework for assertions and not Jasmine, those two have different APIs. Chai syntax uses dot separators, Jasmine methods are camel-cased.

Jasmine has simple API that is covered on a single page, the spec should become

expect(common).toEqual(jasmine.any(Object));

The only case when common variable may become undefined silently is when common service was accidentally overridden to return undefined. In any other case injection error would be thrown.

The injection error in the spec

Error: [$injector:unpr] Unknown provider: loggerProvider <- logger <- common http://errors.angularjs.org/1.5.2/$injector/unpr?p0=loggerProvider%20%3C-%20logger%20%3C-%20common

results from the fact that third-party module files weren't loaded and the modules weren't loaded in common module.

A way to fix this is to load dependencies in spec:

beforeEach(module('loggerModuleName', 'toasterModuleName', 'common'));

But the preferable way is to fix common module itself, because a module should explicitly state its dependencies, not just rely on parent module to load them:

var commonModule = angular.module('common', ['loggerModuleName', 'toasterModuleName']);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!