Angular Unit Tests
MICHAEL HABERMAN
It’s all about insurance!
• Everything that is important or expensive we
cover with insurance
What kind of insurance can
we get?
• Manual testing
• Automation testing
QA
• Human are not 100% reliable
QA test only what they see
Trying the cat thing at home
Can you trust the
developers?
Humans are not objective
• They rush home
• They can’t find defects in their code
• They don’t like criticism
• Actually they hate criticism
• They forget what they did last month
• Actually they forget what they did yesterday
• We need something OBJECTIVE!
Automation testing are
objective
• They can provide a real objective view on our
application
• Lets see what type of automation test can we use
What can we test?
E2E
Integration
Test
Unit Test
Example - AAA
//Arrange
Michael mic = new Michael();
NGConf ng = new NGConf();
mic.Class = ng;
//Act
mic.Speak();
//Assert
Expect(dw.InterestLevel).toBe(10);
What would you test?
var add = function(num1, num2) {
return num1 + num2;
};
// test 1
var result1 = add(1,2);
expect(result1).toBe(3);
//test 2
var result2 = add(‘1’,’2’);
expect(result2).toBe(3);
Unit Testing in Angular
• Three players:
• Process to run the test
• Test runner
• Assertion library / test framework
Hosting process
• There are two options
• Real browser
• Browser simulator / driver
Test runner
• Provide the ability to run the test
• Get result (passed / failed with error)
• Change configuration
• Work with your assertion library
• There are many test runners!
• We will focus on karma
Assertion library
expect(object).toBeArray();
expect(number).toBeOddNumber();
expect(function).toThrowError();
expect(date).toBeBefore(date);
expect(object).toHaveBoolean(memberName);
expect(string).toBeNonEmptyString();
• The syntax you use to write the test
• We will use Jasmine
Test environment
Browser
Host code and
tests
Karma
Node JS server
Connects to each
browser
Reports the
result
Browser
Host code and
tests
Browser
Host code and
tests
Browsers
Host code and
tests
Jasmine
Provides test
syntax
Talked enough! Lets set up the
environment
Karma setup
• Install – npm install karma
• Setup – karma init file_name
• Start – karma start file_name
Karma config file
• Frameworks – Jasmine
• Files – specific or pattern
• autoWatch
• Browsers – multiple is supported
Jasmine
• Describe – a set of tests
• It – a single test
• Expect – single expect
Jasmine - matchers
• Array
• Boolean
• Browser
• Date
• Functions
• Errors
• Numbers
• Objects
• Strings
How to write good unit test?
• Any idea?
• It is not about good testing…
• It all about testable code
Writing testable code
• Isolated objects
• No coupling
• Single responsibility – separation of concern
• Ability to provide mock objects
Handling dependency
Function saveItem(item){
var itemValidator = new itemValidator();
if(!itemValidator.validate(item))
{ return false; }
var fileAccess = new fileAccess();
if(!fileAccess.save(item))
{ return false; }
var notification = new notificationService();
notification.show(“item saved!”);
return true;
}
Handling dependency
Function saveItem(item, itemValidator, fileAccess,
notification){
if(!itemValidator.validate(item))
{ return false; }
if(!fileAccess.save(item))
{ return false; }
notification.show(“item saved!”);
return true;
}
Summary
Thank you!
• E-mail: habmic@gmail.com
• Twitter: @hab_mic
• Blog: http://blogs.microsoft.co.il/michaelh/

Angular Unit Test