I am having a problem were I have a controller in my app which I use like <div ng-controller='LogbookEditCtrl'> </div>
and this controller has a $element provider in it which I need to modify the element.
describe('LogbookEditCtrl', function(){
'use strict';
beforeEach(module('logbooks.edit'));
it('should create "logbook" model', inject(function($controller) {
var scope = {},
// THIS EXPLODES BECAUSE IT SAYS THE $element PROVIDER WAS NOT FOUND, because there
// is no html element of course..the controller is being created on its own.
ctrl = $controller('LogbookEditCtrl', {$scope: scope});
}));
});
I have tried something like the following, but it again says the $element provider was not found :
beforeEach(inject(function(_$element_) {
var element = compile('<div></div>');
$element = element;
}));
AngularJS is written with testability in mind, but it still requires that you do the right thing. We tried to make the right thing easy, but if you ignore these guidelines you may end up with an untestable application.
Approach: To share data between the controllers in AngularJS we have two main cases: Share data between parent and child: Here, the sharing of data can be done simply by using controller inheritance as the scope of a child controller inherits from the scope of the parent controller.
You would need to pass that dependency itself, since it refers to the bound element of the controller. And there is no $element
provider available in angular (much similar to $scope
as these are dynamic special dependencies provided by the app injector). You could create an element object using angular.element and pass it in as locals to the controller constructor,something like this.
var scope = {},
element = angular.element('<div></div>'); //provide element you want to test
ctrl = $controller('LogbookEditCtrl', {$scope: scope, $element:element });
This opens up the fact that why is it not recommended to use $element
inside a controller and perform any DOM operation in them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With