The document discusses strategies for testing AngularJS applications, including unit testing controllers and services, mocking dependencies like HTTP requests and services, and end-to-end testing. It provides examples of writing tests for controllers, services, directives, and end-to-end tests using Protractor. Key points covered are setting up tests, injecting dependencies, mocking, and making assertions to validate test behavior.
15. function AddCtrl() {
var operand1 = $(#operand1);
var operand2 = $(#operand2);
var result = $(#result);
this.add = function() {
result = operand1 + operand2;
}
}
16. var operand1 = $('<input type="text" id="operand1" />');
var operand2 = $('<input type="text" id="operand1" />');
var result = $('<input type="text" id= "result" />');
var span = $('<span>');
$('body').html('<div class="ex1">')
.find('div')
.append(operand1)
.append(operand2)
.append(result);
var ac = new AddCtrl();
operand1.val('1');
operand2.val('1');
ac.add();
expect(result.val()).toEqual('2');
$('body').empty();
17. Controllers - The Angular way
function AddCtrl($scope) {
$scope.Calc = function() {
$scope.result = $scope.operand1 + $scope.operand2;
}
}
18. Controllers - The Angular way
var $scope = {};
var ctrl = $controller(‘AddCtrl’), {$scope: $scope };
$scope.operand1 = 1;
$scope.operand2 = 1;
$scope.calc();
expect($scope.result).toEqual(2);
24. “I get paid for code that works,
not for tests, so my philosophy is
to test as little as possible to reach
a given level of confidence…”
-- Kent Beck
46. describe('Testing simpleDirective', function() {
var scope, elem, directive, compiled, html;
beforeEach(function () {
module('myApp‘);
});
});
it('Should set the text of the element to whatever was passed.',
function() {
});
});
47. describe('Testing simpleDirective', function() {
var scope, elem, directive, compiled, html;
beforeEach(function (){
module('myApp');
html = '<simple-directive value="abc"></simple-directive>';
});
it('Should set the text of the element to whatever was passed.',
function() {
});
});
48. describe('Testing simpleDirective', function() {
var scope, elem, directive, compiled, html;
beforeEach(function (){
module('myApp');
html = ‘<simple-directive value="abc"></simple-directive>';
inject(function($compile, $rootScope) {
scope = $rootScope.$new();
elem = angular.element(html);
compiled = $compile(elem);
compiled(scope);
});
});
it('Should set the text of the element to whatever was passed.',
function() {
});
});
TDD (TDD is dead) – could be a whole talk on its own
Grunt / Karma – though they do provide great value
Unit / e2e testing
Strategies for testing
Lessons learned
Code
examples
Strategic reasons
Helps us fail fast
Safety net – confidence
Helps us understand
Tactical reasons
Dynamically typed language with almost no help from compiler
Strategic reasons
Helps us fail fast
Safety net – confidence
Helps us understand
Tactical reasons
Dynamically typed language with almost no help from compiler
Strategic reasons
Helps us fail fast
Safety net – confidence
Helps us understand
Tactical reasons
Dynamically typed language with almost no help from compiler
Strategic reasons
Helps us fail fast
Safety net – confidence
Helps us understand
Tactical reasons
Dynamically typed language with almost no help from compiler
Javascript can be hard
What does this mean?
- Separation concerns
Need a new image this is terrible
Pattern
IOC
Show something where dependencies are not injected
DI in angular
That the controller doesn’t need the dom
Something that might be written in jquery
Something that we might write to test that.
Lacks readability and hard to understand.
Maybe mention filters and directives
Format expressions
What do you use it for…
Why would you use it…
How do you use it?
Behaviors vs. correctness
brittleness