Is there a way to properly test this module using jasmine? Testing the $controller
function has proven to be challenging due to its encapsulation within a closure, making testing difficult.
Essentially, with the module definition provided below, attempting to write a unit test for MainCtrl seems fruitless.
(function () {
'use strict';
angular.module('app', []);
function MainCtrl() {
var mc = this;
mc.obj = {
val : 50
};
}
angular.module('app').controller('MainCtrl', MainCtrl);
}());
and the "standard" jasmine test
describe('app', function(){
beforeEach(module('app'));
it('should create an object with val 50', inject(function(_$controller_) {
var scope = {},
ctrl = _$controller_('MainCtrl', {$scope:scope});
expect(scope.obj.val).toBe(50); // returns Expected undefined to be 50.
}));
});
When angular injects the _$controller_
service within the jasmine test function, the controller instance created results in an undefined $scope.
How can this be properly tested?
My search for a solution to this issue on StackOverflow did not yield the desired result, so I decided to implement my own solution.