From be1e6fde54c7fba2b1c92df3593dcb5395432427 Mon Sep 17 00:00:00 2001 From: Andy Gurden Date: Thu, 3 Dec 2015 00:53:35 +0000 Subject: [PATCH] feat(ngMock): destroy $rootScope after each test Previously $rootScope would be new for each test, but old $rootScopes would never be destroyed. Now that we are able to destroy the $rootScope, doing so provides an opportunity for code to clean up things like long-lived event handlers between tests. --- src/ngMock/angular-mocks.js | 1 + test/ngMock/angular-mocksSpec.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 6bbb7a651db7..ce0dfdfe035c 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -2480,6 +2480,7 @@ if (window.jasmine || window.mocha) { if (injector) { injector.get('$rootElement').off(); + injector.get('$rootScope').$destroy(); } // clean up jquery's fragment cache diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 806b37036dcf..5d2732a66eb2 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1650,6 +1650,21 @@ describe('ngMock', function() { }); + describe('$rootScope', function() { + var destroyed = false; + + it('should destroy $rootScope after each test', inject(function($rootScope) { + $rootScope.$on('$destroy', function() { + destroyed = true; + }); + })); + + it('should have destroyed the $rootScope from the previous test', function() { + expect(destroyed).toBe(true); + }); + }); + + describe('$rootScopeDecorator', function() { describe('$countChildScopes', function() {