sample document:
describe('$rootScope', function() {
describe('$on', function() {
var credentials = "Basic abcd1234";
var $scope;
var $rootScope;
var $httpBackend;
...
beforeEach(inject(function($injector, $controller, _$rootScope_, $state, _$q_, currentUserService) {
$scope = _$rootScope_.$new();
$rootScope = _$rootScope_;
$httpBackend.when('GET', 'dist/app/login/login.html').respond({'title': 'TEST_TITLE'}, {'A-Token': 'xxx'});
}));
...
it('should set $scope.title if noAuthorization', function() {
spyOn($rootScope, '$on');
$controller('AppCtrl', {$scope: $scope});
$rootScope.$broadcast("$stateChangeStart");
$rootScope.$apply();
expect($rootScope.$on).toHaveBeenCalled();
expect($scope.title).toBe('TEST_TITLE');
});
});
});
$scope.title always remains undefined during my tests. The expectation consistently fails. I have attempted using $emit, $apply, and other methods. This issue arises within a controller, nested within a $rootScope.on function.
Interestingly, when I log $scope.title in the javascript file, it exhibits the updated value.
It's worth noting that the function being invoked is not within $scope, meaning it's not $scope.updateTitle
, but simply function updateTitle(...)
I don't believe displaying the actual code is necessary as it functions correctly. I am simply perplexed as to why the $scope isn't updating in the tests.