I have this code in my controller:
angular.module('clientApp')
.controller('UserSettingsAccountCtrl', function ($scope, userFactory) {
$scope.$watch(function() {
return userFactory.getUser();
}, function(value) {
$scope.user = value;
console.log($scope.user) //defined when I add scope.$digest() in my test
}
});
console.log($scope.user) //undefined
});
It's executed when my controller is instantiated. But in my test it's not. I get user undefined.
beforeEach(inject(function ($controller, $rootScope, userFactory) {
scope = $rootScope.$new();
var mockUser = {
userId: 1,
name: 'Per',
premium:{
active:false,
freeTrial:{
expired:{
value:false
}
}
}
};
userFactory.setUser(mockUser);
console.log(userFactory.getUser()); //User is returned
UserSettingsAccountCtrl = $controller('UserSettingsAccountCtrl', {
$scope: scope,
});
}));
it('should be ok to start a trial if the trial has not expired and the user is not a premium', function(){
console.log(scope.user) //undefined
...
});
Why is this executed when I run it live but not in my test?