Transcript of Angular Unit Test
- 1. Angular Unit Tests MICHAEL HABERMAN
- 2. Its all about insurance! Everything that is important or
expensive we cover with insurance
- 3. What kind of insurance can we get? Manual testing Automation
testing
- 4. QA Human are not 100% reliable
- 5. QA test only what they see
- 6. Trying the cat thing at home
- 7. Can you trust the developers?
- 8. Humans are not objective They rush home They cant find
defects in their code They dont like criticism Actually they hate
criticism They forget what they did last month Actually they forget
what they did yesterday We need something OBJECTIVE!
- 9. Automation testing are objective They can provide a real
objective view on our application Lets see what type of automation
test can we use
- 10. What can we test? E2E Integration Test Unit Test
- 11. Example - AAA //Arrange Michael mic = new Michael(); NGConf
ng = new NGConf(); mic.Class = ng; //Act mic.Speak(); //Assert
Expect(dw.InterestLevel).toBe(10);
- 12. 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);
- 13. Unit Testing in Angular Three players: Process to run the
test Test runner Assertion library / test framework
- 14. Hosting process There are two options Real browser Browser
simulator / driver
- 15. 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
- 16. 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
- 17. 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
- 18. Talked enough! Lets set up the environment
- 19. Karma setup Install npm install karma Setup karma init
file_name Start karma start file_name
- 20. Karma config file Frameworks Jasmine Files specific or
pattern autoWatch Browsers multiple is supported
- 21. Jasmine Describe a set of tests It a single test Expect
single expect
- 22. Jasmine - matchers Array Boolean Browser Date Functions
Errors Numbers Objects Strings
- 23. How to write good unit test? Any idea? It is not about good
testing It all about testable code
- 24. Writing testable code Isolated objects No coupling Single
responsibility separation of concern Ability to provide mock
objects
- 25. 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; }
- 26. 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; }
- 27. Summary
- 28. Thank you! E-mail: habmic@gmail.com Twitter: @hab_mic Blog:
http://blogs.microsoft.co.il/michaelh/