This document discusses unit testing in AngularJS. It introduces Karma as a test runner for JavaScript unit tests. It also covers popular testing frameworks like Jasmine, Mocha and QUnit that can be used with Karma. It demonstrates how to unit test AngularJS components like controllers, services and directives. Finally, it discusses coverage reports that can be generated with Karma and Istanbul to track testing coverage.
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Unit Testing in AngularJS - CC FE & UX
1. UNIT TESTING IN ANGULARJS
Dennis Jaamann — Frédéric Ghijselinck
—@dennisjaamann @f_ghijselinck
CC Front-end & UX
2. UNIT TESTING IN ANGULARJS
Unit testing JavaScript
Karma
Unit testing frameworks
QUnit
Mocha
Jasmine
Unit testing AngularJS
a. Controller
b. Service
c. Directive
Coverage reports
3. UNIT TESTING JAVASCRIPT
Testing JavaScript is hard
Mixed with HTML
Inline scripts
No real classes
No real modules
Feels like wrestling a king croc
4. UNIT TESTING JAVASCRIPT - PITFALLS
DOM interactions
AJAX
Event handlers
Timeouts / Intervals
Promises
Basically everything asynchronous
5. UNIT TESTING JAVASCRIPT - WHAT DO WE NEED?
Browser
Test Runner
Test framework
Assertions (matchers)
Ways to mock
...
6. KARMA
Karma = unit test runner (by angular core team)
configuration file
test suites
headless/real browser
Install karma
npm install karma savedev
Automatically generate a config file
karma init
15. JASMINE
▪ test suite with specs:
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
describe("A suite is just a function", function() {
var a;
it("and so is a spec", function() {
a = true;
expect(a).toBe(true);
});
});
21. JASMINE - DISABLE
disabling suites with xdescribe
xdescribe("A disabled suite", function() {
it("with a spec", function() {
expect(true).toBe(true);
});
});
disabling specs with xit
describe("A suite", function() {
xit("with a disabled spec", function() {
expect(true).toBe(true);
});
});
22. JASMINE - EXCLUSIVE TESTS
run specific suites with ddescribe
ddescribe("An exclusive run suite", function() {
it("with a spec", function() {
expect(true).toBe(true);
});
});
run specific specs with iit
describe("A suite", function() {
iit("with a exclusive run spec", function() {
expect(true).toBe(true);
});
});
23. UNIT TESTING ANGULARJS
Angular = separation of concerns
Create highly cohesive, low coupled pieces of functionality
Easier to test
24. UNIT TESTING ANGULARJS
getting the module
injecting the controller
beforeEach(module('app'));
beforeEach(inject(function($controller, $rootScope) {
controller = $controller;
scope = $rootscope.$new();
}));