Unit Testing with Jasmine in Angular.
HTML
<script src="http://daniellmb.github.com/jasmine-run/jasmine-1.3.1-run.js"></script>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-mocks.js"></script>
JavaScript
//AngularJS Directive with Isolated Scope
angular.module('myApp', [])
.directive('myDirective', function(){
return {
link: function(scope, element, attr, controller) {
}
};
});
//Directive Unit Tests
describe('myDirective:', function () {
var rootScope, compile;
beforeEach(module('myApp'));
beforeEach(function () {
//inject dependencies
inject(function ($compile, $rootScope) {
rootScope = $rootScope;
compile = $compile;
});
});
it('first test', function () {
var scope = rootScope.$new();
var element = angular.element('<my-directive><div id="myid" style="height:200px"></div></my-directive>');
element.appendTo(document.body);
element = compile(element)(scope);
console.log("#myid height: "+$("#myid").height());
expect($("#myid").height()).toBe(200);
element.remove();
});
it('second test', function () {
var scope = rootScope.$new();
var element = angular.element('<my-directive><div id="myid" style="height:400px"></div></my-directive>');
element.appendTo(document.body);
element = compile(element)(scope);
console.log("#myid height: "+$("#myid").height());
expect($("#myid").height()).toBe(400);
});
});
//*/