简体   繁体   中英

When testing Angular services with Jasmine, is the inclusion order of scripts causing this unknown provider error

I have an Angular service that is including two other modules: logger and toaster . logger.js is local and I've included it in this plunkr . As for toaster it is a third party library, and within the plunker, index.html is including it as a script tag.

This is my first attempt at testing with Jasmine, and I've trimmed things down to be concise. At first I couldn't get the tests to pass due to an injector error related to the logger module. Finally through trial and error, I noticed that, in index.html, if I loaded logger.js after common.js, the error would go away and the tests would pass.

Now I am trying to pass the toaster module into the common service module, but no matter where I include the reference to toaster.min.js, I get the following error. When I run this locally instead of as a plunkr, I can verify that toaster.min.js is getting loaded; furthermore by looking at its content, I've verified that it defines a toaster module. So I am kind of at a loss as to what else to try in order to get toaster to load properly so I can finish the testing of my common service.

Error: [$injector:unpr] Unknown provider: toasterProvider <- toaster <- common

If it's not the order of including the modules via their script/src tags, any other help would be appreciated, up to and including forking and modifying the plunker so it runs.

you need to inject the following modules first

beforeEach(module('toaster'));

then

 beforeEach(module('common'));

The module can be successfully used in production and testing if the dependencies are loaded properly for the module:

var commonModule = angular.module('common', ['toaster']);

//runs without error: commonModule.factory('common',['logger', common]);
commonModule.factory('common',['logger', 'toaster', common]);

common service from commonModule module depends on toaster service from toaster module, hence commonModule should load toaster module as a dependency.

The same applies to any services that aren't defined in the current module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM