How do I test a scope variable that gets changed by an event?
This is the bit of code I want to test:
$rootScope.$on('someEvent', function(event, option) {
$scope.option = option;
});
I'm trying to do
describe("`someEvent`", function() {
var option = 'Title',
beforeEach(inject(function($injector) {
$scope.$emit('someEvent', option);
}));
it("updates option", function() {
expect($scope.option).toBe(option);
});
});
I've also tried with $timeout
describe("`someEvent`", function() {
var option = 'Title',
$timeout;
beforeEach(inject(function($injector) {
$timeout = $injector.get('$timeout');
$timeout(function() {
$scope.$emit('someEvent', option);
});
$timeout.flush();
}));
it("updates option", function() {
expect($scope.option).toBe(option);
});
});
and
describe("`someEvent`", function() {
var option = 'Title';
beforeEach(inject(function($injector) {
var $rootScope = $injector.get('$rootScope');
$rootScope.$broadcast('someEvent', option);
}));
it("updates option", function() {
expect($scope.option).toBe(option);
});
});
But they all fail with the message Expected undefined to be 'Title'.
I'm guessing because it's async, and I need some other syntax for it. I'm using jasmine.
Any ideas?
Edit: Updated with more attempts
Fixed: It was because the describe
block was declared in the wrong area, where $scope
had not been set up. Pretty much the same as How do I unit test $scope.$emit changing a scope variable in angularjs?, so closing it.