Unknown $rootElementProvider: Qunit + angularjs in

2019-04-07 03:11发布

问题:

When I try to inject $location service in unit test (qunit) I get error: Unknown $rootElementProvider <- $rootElement <- $location

Other things without $location service dependency are injected correctly.

 var $injector = angular.injector(['ng', 'myApp']);
 var $location = $injector.get('$location');

source: http://jsfiddle.net/H4qGb/5/

One more strange error happens in the second test.

Does anybody know how to integrate angular with qunit best way?

回答1:

You should be using the angularjs mocks.

http://code.angularjs.org/1.1.5/angular-mocks.js

You can then instruct your injector to also use these mocks.
var $injector = angular.injector(['ngMock','ng', 'myApp']);

http://jsfiddle.net/jxVDT/