This is a continuation of another question I asked that was successfully answered.
I'm learning how to unit test AngularJS applications with Karma, Jasmine, and ngMock. Here's the code I have a question about:
describe('myController function', function() {
describe('myController', function() {
var scope;
beforeEach(module('myApp'));
beforeEach(inject(function($rootScope, $controller) {
// These are the 2 lines I'm a bit confused about:
scope = $rootScope.$new();
$controller('MyController', {$scope: scope});
}));
it("...");
});
});
Question 1: Why do we create a new scope
and include it in the locals injection area in this line: $controller('MyController', {$scope: scope});
? It seems to work just fine (as in, scope
now represents the $scope
object from that controller and has all the properties and functions it's supposed to), but the code seems to imply that we're resetting the controller's $scope
with our newly-created (and empty) scope
(from the line scope = $rootScope.$new();
). So I think I'm just not fully understanding the purpose/inner-workings of those locals.
Question 2: I also have seen from my searching that new scope
gets created in 2 common ways, either the way shown above with $rootScope.$new()
, or by simply declaring scope = {}
. Even the Angular docs do it both ways, as seen here (using $rootScope.$new()
) and here (using $scope = {}
. Why do this one way over another? Is there a difference?