Example of different scoping in tests
Broken test showing folly of not creating a new scope for tests, isolate or otherwise.
by IgorMinar
HTML
<script src="http://ci.angularjs.org/job/angular.js-angular-master/ws/build/angular.js"></script>
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine.js"></script>
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine-html.js"></script>
<script src="http://ci.angularjs.org/job/angular.js-angular-master/232/artifact/build/pkg/0.10.7-64912069/angular-mocks-0.10.7-64912069.js"></script>
<link rel="stylesheet" href="http://tryjasmine.com/js/vendor/jasmine-1.1.0/jasmine.css">
<div ng-app="myModule" ng-init="myState = 'hey!'">
<div my-state="myState"></div>
</div>
JavaScript
angular.module("myModule", []).directive('myState', function($compile) {
return {
scope: {
myState: 'accessor'
},
template: '<span>{{myState()}}</span>'
}
});
describe('with rootScope WRONG', function() {
var directiveScope, element, compile;
beforeEach(module('myModule')); // Define the module context for the test
it('myState', inject(function($rootScope, $compile) {
var myState = $rootScope.mmyState = 33
element = $compile('<div my-state="mmyState"></div>')($rootScope);
$rootScope.$apply();
expect($rootScope.mmyState).toBe(myState);
expect($rootScope.myState).toBeUndefined();
expect(element.text()).toBe("33");
}));
});
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var trivialReporter = new jasmine.TrivialReporter();
jasmineEnv.addReporter(trivialReporter);
jasmineEnv.specFilter = function(spec) {
return trivialReporter.specFilter(spec);
};
$(function() {
jasmineEnv.execute();
});
})();