On Github andimarek / angular-testing
By Andi Marek / @andimarek
errare humanum estBased on Return on investment (ROI)
From Top to Bottom: Less integrated
Image by Martin FowlerTest Runner: Karma
Test Framework: Jasmine
Headless Browser: PhantomJS
Testing the smallest possible scope (lines of code).
Best way to write Unit Tests: Test Driven Development (TDD).
Principle: Testing in Isolation -> Dependencies are mocked.
Is it still working?
Documentation
Jasmine
Karma
describe('Testing myService', function () { var myService; var aDependencyMock; beforeEach(function () { aDependencyMock = { someFunction: function () { } }; module('myModule.myService'); module(function ($provide) { $provide.value('aDependency', aDependencyMock); }); inject(function (_myService_) { myService = _myService_; }); }); it('service should be defined', function () { expect(myService).toBeDefined(); }); });
A part of the app = Directive
Integration Test
No Backend Calls or external dependencies
Jasmine
Karma
describe('Testing myDirective', function () { var element; var $scope; beforeEach(function () { module('myModule.myDirectice'); inject(function ($compile, $rootScope) { $scope = $rootScope.$new(); element = angular.element('<myDirective/>'); $compile(element)($scope); $scope.$digest(); }); }); it('now test element', function () { }); });
it('should call methodX on click', function () { spyOn($scope, 'methodX'); element.find('.button').click(); expect($scope.methodX).toHaveBeenCalled(); });
Integrated with Angular: Knows when the app is ready
Jasmine like syntax
Protractor: Successor based on Webdriver
Karma
describe('E2E Test', function () { beforeEach(function () { browser().navigateTo('http://localhost/url/to/test/index.html'); }); it("should show a dialog", function () { element('.show-dialog').click(); expect(element(".dialog").count()).toBe(1); }); });
700 Unit vs 100 Directive vs 25 E2E
Prefer the bottom of the pyramid
Every pyramid looks different