I am testing a directive with isolate scope (angular) and Jasmine test framework. Everything is working fine with one oddity: if I change the variable type on $scope from string to number, Jasmine continues to see it as a string even though the directive code itself properly treats it as a number.
I understand why this might make intuitive sense - the parm is interpolated initially as a string and maybe Angular has some mechanism for ensuring it stays a string. If true, I don't get how and where it is implemented in the Angular source (or even how it could be).
In sum: Why does Jasmine see a string and the directive see a number?
Here's my code:
Markup
<pane id={{myId}}></pane>
Directive
.directive('testPane', function () {
function thisController($scope) {
//$scope.id is string '1'
$scope.id = parseInt($scope.id);
//now it is number 1 (as expected)
console.log($scope.id);
};
return {
restrict: 'E',
transclude: true,
scope: { id: '@'},
controller: thisController,
link: function (scope, element, attrs, tabsController) {
},
templateUrl: 'test/test.html',
replace: true
};
})
Jasmine Spec
it("changes id from string to number", function () {
var element,
elementScope;
$rootScope.myId = 1;
element = $compile(html)($rootScope);
$timeout(function () {
businessTabScope = element.isolateScope();
$rootScope.$digest();
expect(elementScope.id).toBeDefined();
//id is text '1' instead of number 1. Why?
expect(elementScope.id).toEqual($rootScope.myId);
}, 0, false);
$timeout.flush();
});
$timeouthere... Have you tried adding log statements to see which code is executing? Have you tried increasing the timeout value? By the way, I've never had to use$timeoutwhen testing my directives ... I just call$digest()on the directive's parent scope.