SlideShare a Scribd company logo
Angular Unit Testing
Shailendra Chauhan
Founder & CEO - Dot Net Tricks
#ngIndia www.ng-ind.com Feb 24, 2018
Microsoft MVP
ng-India
• What is Unit Testing?
• Angular CLI Setup
• Angular Testing Tools
• Jasmine Test Spec
• Setup and Teardown
• Testing A Simple Component
• Debugging and Code Coverage
• Testing A Component with Template and Dependencies
• Testing Http Service
Agenda
• A unit testing is a method, where each unit or component of a
software is tested to determine whether it is fit for use or not
• A single unit is any block of code (i.e. function or class) that
has one and only one responsibility
• A function might have multiple unit tests according to the uses
and output of the function
What is Unit Testing?
Angular CLI setup
www.nodejs.org
Node 6.9+
npm install -g @angular/cli
Angular CLI
ng new appName
Create New Project
ng test
Run Test
Angular Testing Tools
Test Runner
Karma
Test Framework
Jasmine
Test Utilities
Angular (TestBed,
ComponentFixture)
Jasmine Test Spec
describe(str, fn)
• A Test Suite
• Contains Test Specs
it(str, fn)
• A Test Spec
• Contains 1 or more
test expectations
expect(actual)
• An expected piece
of behavior
matcher(expected)
• Does a boolean
comparison
• toEqual, toContain,
toBeNull
Setup and Teardown(cleaning up)
beforeAll()
• Called once, before
all the specs in a
test suite run
beforeEach()
• Called before each
test spec run
afterAll()
• Called once, after
all the specs in a
test suite finished
afterEach()
• Called after each
test spec run
Testing A Simple Component
Debugging
Code Coverage
Testing A Component with Template
Testing A Component with Dependencies
Testing Http Service
Skill Challenge
Thanks!

More Related Content

What's hot

An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
Selenium Locators
Selenium LocatorsSelenium Locators
Selenium Locators
Satyam Pandey
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
NexThoughts Technologies
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
Levon Apreyan
 
Selenium locators: ID, Name, xpath, CSS Selector advance methods
Selenium locators: ID, Name,  xpath, CSS Selector advance methodsSelenium locators: ID, Name,  xpath, CSS Selector advance methods
Selenium locators: ID, Name, xpath, CSS Selector advance methods
Pankaj Dubey
 
Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
Return on Intelligence
 
Selenium with java
Selenium with javaSelenium with java
Selenium with java
Gousalya Ramachandran
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
Bethmi Gunasekara
 
Hybrid automation framework
Hybrid automation frameworkHybrid automation framework
Hybrid automation framework
doai tran
 
Postman.ppt
Postman.pptPostman.ppt
Postman.ppt
ParrotBAD
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
Udaya Kumar
 
Selenium
SeleniumSelenium
Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
Junit
JunitJunit
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
Cuelogic Technologies Pvt. Ltd.
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
Gousalya Ramachandran
 
Setting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation FrameworkSetting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation Framework
valuebound
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
Denis Bazhin
 

What's hot (20)

An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Selenium Locators
Selenium LocatorsSelenium Locators
Selenium Locators
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Selenium locators: ID, Name, xpath, CSS Selector advance methods
Selenium locators: ID, Name,  xpath, CSS Selector advance methodsSelenium locators: ID, Name,  xpath, CSS Selector advance methods
Selenium locators: ID, Name, xpath, CSS Selector advance methods
 
Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
 
Selenium with java
Selenium with javaSelenium with java
Selenium with java
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Hybrid automation framework
Hybrid automation frameworkHybrid automation framework
Hybrid automation framework
 
Postman.ppt
Postman.pptPostman.ppt
Postman.ppt
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
Selenium
SeleniumSelenium
Selenium
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Junit
JunitJunit
Junit
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
Setting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation FrameworkSetting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation Framework
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 

Similar to Angular Unit Testing

Tools for Software Testing
Tools for Software TestingTools for Software Testing
Tools for Software Testing
Mohammed Moishin
 
Beginners overview of automated testing with Rspec
Beginners overview of automated testing with RspecBeginners overview of automated testing with Rspec
Beginners overview of automated testing with Rspec
jeffrey1ross
 
software testing
 software testing software testing
software testing
Sara shall
 
Lecture #6. automation testing (andrey oleynik)
Lecture #6. automation testing (andrey oleynik)Lecture #6. automation testing (andrey oleynik)
Lecture #6. automation testing (andrey oleynik)
Andrey Oleynik
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in Salesforce
Jitendra Zaa
 
"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl
TEST Huddle
 
Test automation within a scrum process
Test automation within a scrum processTest automation within a scrum process
Test automation within a scrum process
Kushan Shalindra Amarasiri - Technical QE Specialist
 
Test automation proposal
Test automation proposalTest automation proposal
Test automation proposal
Mihai-Cristian Fratila
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
weili_at_slideshare
 
What is an Automation Framework ?
What is an Automation Framework ?�What is an Automation Framework ?�
What is an Automation Framework ?
Sriram Angajala
 
Level Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingLevel Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit Testing
Gordon Bockus
 
Sustainable agile testing
Sustainable agile testingSustainable agile testing
Sustainable agile testing
mimmozzo_
 
xUnit test patterns 1
xUnit test patterns 1xUnit test patterns 1
xUnit test patterns 1
Stanislav Petrov
 
Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad
Sathya Technologies
 
Selenium course training institute ameerpet hyderabad – Best software trainin...
Selenium course training institute ameerpet hyderabad – Best software trainin...Selenium course training institute ameerpet hyderabad – Best software trainin...
Selenium course training institute ameerpet hyderabad – Best software trainin...
Sathya Technologies
 
Quality Management Introduction
Quality Management IntroductionQuality Management Introduction
Quality Management Introduction
Duy Tan Geek
 
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f..." Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
Lohika_Odessa_TechTalks
 
Testing Flex RIAs for NJ Flex user group
Testing Flex RIAs for NJ Flex user groupTesting Flex RIAs for NJ Flex user group
Testing Flex RIAs for NJ Flex user group
Viktor Gamov
 
Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptx
RiyaBangera
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to Protractor
Florian Fesseler
 

Similar to Angular Unit Testing (20)

Tools for Software Testing
Tools for Software TestingTools for Software Testing
Tools for Software Testing
 
Beginners overview of automated testing with Rspec
Beginners overview of automated testing with RspecBeginners overview of automated testing with Rspec
Beginners overview of automated testing with Rspec
 
software testing
 software testing software testing
software testing
 
Lecture #6. automation testing (andrey oleynik)
Lecture #6. automation testing (andrey oleynik)Lecture #6. automation testing (andrey oleynik)
Lecture #6. automation testing (andrey oleynik)
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in Salesforce
 
"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl
 
Test automation within a scrum process
Test automation within a scrum processTest automation within a scrum process
Test automation within a scrum process
 
Test automation proposal
Test automation proposalTest automation proposal
Test automation proposal
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
 
What is an Automation Framework ?
What is an Automation Framework ?�What is an Automation Framework ?�
What is an Automation Framework ?
 
Level Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingLevel Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit Testing
 
Sustainable agile testing
Sustainable agile testingSustainable agile testing
Sustainable agile testing
 
xUnit test patterns 1
xUnit test patterns 1xUnit test patterns 1
xUnit test patterns 1
 
Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad
 
Selenium course training institute ameerpet hyderabad – Best software trainin...
Selenium course training institute ameerpet hyderabad – Best software trainin...Selenium course training institute ameerpet hyderabad – Best software trainin...
Selenium course training institute ameerpet hyderabad – Best software trainin...
 
Quality Management Introduction
Quality Management IntroductionQuality Management Introduction
Quality Management Introduction
 
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f..." Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
 
Testing Flex RIAs for NJ Flex user group
Testing Flex RIAs for NJ Flex user groupTesting Flex RIAs for NJ Flex user group
Testing Flex RIAs for NJ Flex user group
 
Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptx
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to Protractor
 

More from Shailendra Chauhan

Mastering Best Azure DevOps Online Training and Certification - Dot Net Tricks
Mastering Best Azure DevOps Online Training and Certification - Dot Net TricksMastering Best Azure DevOps Online Training and Certification - Dot Net Tricks
Mastering Best Azure DevOps Online Training and Certification - Dot Net Tricks
Shailendra Chauhan
 
Introduction to Docker | Docker and Kubernetes Training
Introduction to Docker | Docker and Kubernetes TrainingIntroduction to Docker | Docker and Kubernetes Training
Introduction to Docker | Docker and Kubernetes Training
Shailendra Chauhan
 
Azure for.Net developers | Azure Online Certification Training
Azure for.Net developers | Azure Online Certification TrainingAzure for.Net developers | Azure Online Certification Training
Azure for.Net developers | Azure Online Certification Training
Shailendra Chauhan
 
Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 |
Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 | Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 |
Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 |
Shailendra Chauhan
 
Get Started with ASP.NET Core Training, Tutorial - Beginner to Advance
Get Started with ASP.NET Core Training, Tutorial - Beginner to AdvanceGet Started with ASP.NET Core Training, Tutorial - Beginner to Advance
Get Started with ASP.NET Core Training, Tutorial - Beginner to Advance
Shailendra Chauhan
 
Microservices Tutorial for Beginners | All You Need to Get Started
Microservices Tutorial for Beginners | All You Need to Get StartedMicroservices Tutorial for Beginners | All You Need to Get Started
Microservices Tutorial for Beginners | All You Need to Get Started
Shailendra Chauhan
 
Web Applications Development with MEAN Stack
Web Applications Development with MEAN StackWeb Applications Development with MEAN Stack
Web Applications Development with MEAN Stack
Shailendra Chauhan
 
Building Cross Platform Mobile Apps
Building Cross Platform Mobile AppsBuilding Cross Platform Mobile Apps
Building Cross Platform Mobile Apps
Shailendra Chauhan
 
ASP.NET MVC Interview Questions and Answers by Shailendra Chauhan
ASP.NET MVC Interview Questions and Answers by Shailendra ChauhanASP.NET MVC Interview Questions and Answers by Shailendra Chauhan
ASP.NET MVC Interview Questions and Answers by Shailendra Chauhan
Shailendra Chauhan
 

More from Shailendra Chauhan (9)

Mastering Best Azure DevOps Online Training and Certification - Dot Net Tricks
Mastering Best Azure DevOps Online Training and Certification - Dot Net TricksMastering Best Azure DevOps Online Training and Certification - Dot Net Tricks
Mastering Best Azure DevOps Online Training and Certification - Dot Net Tricks
 
Introduction to Docker | Docker and Kubernetes Training
Introduction to Docker | Docker and Kubernetes TrainingIntroduction to Docker | Docker and Kubernetes Training
Introduction to Docker | Docker and Kubernetes Training
 
Azure for.Net developers | Azure Online Certification Training
Azure for.Net developers | Azure Online Certification TrainingAzure for.Net developers | Azure Online Certification Training
Azure for.Net developers | Azure Online Certification Training
 
Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 |
Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 | Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 |
Best Angular Training Online: Angular tutorial | Learn Angular 2 to 10 |
 
Get Started with ASP.NET Core Training, Tutorial - Beginner to Advance
Get Started with ASP.NET Core Training, Tutorial - Beginner to AdvanceGet Started with ASP.NET Core Training, Tutorial - Beginner to Advance
Get Started with ASP.NET Core Training, Tutorial - Beginner to Advance
 
Microservices Tutorial for Beginners | All You Need to Get Started
Microservices Tutorial for Beginners | All You Need to Get StartedMicroservices Tutorial for Beginners | All You Need to Get Started
Microservices Tutorial for Beginners | All You Need to Get Started
 
Web Applications Development with MEAN Stack
Web Applications Development with MEAN StackWeb Applications Development with MEAN Stack
Web Applications Development with MEAN Stack
 
Building Cross Platform Mobile Apps
Building Cross Platform Mobile AppsBuilding Cross Platform Mobile Apps
Building Cross Platform Mobile Apps
 
ASP.NET MVC Interview Questions and Answers by Shailendra Chauhan
ASP.NET MVC Interview Questions and Answers by Shailendra ChauhanASP.NET MVC Interview Questions and Answers by Shailendra Chauhan
ASP.NET MVC Interview Questions and Answers by Shailendra Chauhan
 

Recently uploaded

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 

Recently uploaded (20)

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 

Angular Unit Testing

  • 1. Angular Unit Testing Shailendra Chauhan Founder & CEO - Dot Net Tricks #ngIndia www.ng-ind.com Feb 24, 2018 Microsoft MVP ng-India
  • 2. • What is Unit Testing? • Angular CLI Setup • Angular Testing Tools • Jasmine Test Spec • Setup and Teardown • Testing A Simple Component • Debugging and Code Coverage • Testing A Component with Template and Dependencies • Testing Http Service Agenda
  • 3. • A unit testing is a method, where each unit or component of a software is tested to determine whether it is fit for use or not • A single unit is any block of code (i.e. function or class) that has one and only one responsibility • A function might have multiple unit tests according to the uses and output of the function What is Unit Testing?
  • 4. Angular CLI setup www.nodejs.org Node 6.9+ npm install -g @angular/cli Angular CLI ng new appName Create New Project ng test Run Test
  • 5. Angular Testing Tools Test Runner Karma Test Framework Jasmine Test Utilities Angular (TestBed, ComponentFixture)
  • 6. Jasmine Test Spec describe(str, fn) • A Test Suite • Contains Test Specs it(str, fn) • A Test Spec • Contains 1 or more test expectations expect(actual) • An expected piece of behavior matcher(expected) • Does a boolean comparison • toEqual, toContain, toBeNull
  • 7. Setup and Teardown(cleaning up) beforeAll() • Called once, before all the specs in a test suite run beforeEach() • Called before each test spec run afterAll() • Called once, after all the specs in a test suite finished afterEach() • Called after each test spec run
  • 8. Testing A Simple Component
  • 11. Testing A Component with Template
  • 12. Testing A Component with Dependencies

Editor's Notes

  1. End-to-end tests explore the application as users experience it.
  2. Jasmine - It provides everything needed to write basic tests. It ships with an HTML test runner that executes tests in the browser. Karma - It is ideal for writing and running unit tests while developing the application. Protractor - It is used to write and run end-to-end (e2e) tests. In e2e testing, one process runs the real application and a second process runs protractor tests that simulate user behavior and assert that the application respond in the browser as expected. End-to-end tests explore the application as users experience it.
  3. Jasmine - It provides everything needed to write basic tests. It ships with an HTML test runner that executes tests in the browser. Karma - It is ideal for writing and running unit tests while developing the application. Protractor - It is used to write and run end-to-end (e2e) tests. In e2e testing, one process runs the real application and a second process runs protractor tests that simulate user behavior and assert that the application respond in the browser as expected. End-to-end tests explore the application as users experience it. Karma – A test runner for running unit tests. Jasmine – A framework for writing basic tests. It ships with an HTML test runner that executes tests in the browser. Test Utilities – Angular provides classes like TestBed, Component Fixture, Mock backend and several helper functions for unit tests.
  4. The describe(string, function) defines a collection of Test Specs, called as Test Suite The it(string, function) defines a Test Spec, containing one or more Test expectations The expect(actual) describes an expected piece of behaviour in the application The matcher(expected) like toEqual, toContain, toBeNull does a boolean comparison of the expected value with actual value You can do negative assertion with not describe('Hello world', () => { it('says hello', () => { expect(helloWorld()) .toEqual('Hello world!'); }); it('not equal to Hello', () => { expect(helloWorld()) .not.toEqual('Hello!'); }); });
  5. The describe(string, function) defines a collection of Test Specs, called as Test Suite The it(string, function) defines a Test Spec, containing one or more Test expectations The expect(actual) describes an expected piece of behaviour in the application The matcher(expected) like toEqual, toContain, toBeNull does a boolean comparison of the expected value with actual value You can do negative assertion with not describe('Hello world', () => { it('says hello', () => { expect(helloWorld()) .toEqual('Hello world!'); }); it('not equal to Hello', () => { expect(helloWorld()) .not.toEqual('Hello!'); }); });
  6. Testbed configures and initializes environment for unit testing and provides methods for creating components and services in unit tests. component fixture helps you to test and debug a component class and it’s dom nativeElement is a reference to the DOM element DebugElement is an Angular2 class that contains all kinds of references and methods, to investigate an element or component Async() will allow the next test not to start until the async finishes all it's tasks. It tells to the test framework to wait until the return promise or observable is completed before treating the test as completed. fixture.detectChanges() : trigger change detection toBeTruthy() - Check if the value, when cast to a boolean, will be a truthy value i.e. Truthy values are all values that aren't 0, '' (empty string), false, null, NaN, undefinedor [] (empty array)*. To test if something evaluates to true, you use the toBeTruthy matcher: expect(true).toBeTruthy(); expect(12).toBeTruthy(); expect({}).toBeTruthy(); Likewise, to test if something evaluates to false, you use toBeFalsy: expect(false).toBeFalsy(); expect(null).toBeFalsy(); expect("").toBeFalsy(); WebPack developers need not to call compileComponents since it inlines templates and css as part of the automated build process that precedes running the test. Component = new componentclass() vs component-fixture-createinstance Isolated tests are for when you only want to test the internal behavior of the class. For instance class MyComponent { doSomething(): string {} } let component = new MyComponent(); expect(component.doSomething()).toBe('Hello World'); Here you're only going to test the behavior of the doSomething method. That's it. With the isolated tests you can't test the DOM interaction, as the template is never compiled. For this we should let Angular create the component. Then the component will go through through the actual lifecycle it would go through as it it was in the real application ng test --watch - To watch and re-run only the tests which have changed ng test --coverage - Free code coverage report
  7. Spy is a jasmine feature, where we skip the original object/class method call and pass your own implementation. Testbed configures and initializes environment for unit testing and provides methods for creating components and services in unit tests. component fixture helps you to test and debug a component class and it’s dom nativeElement is a reference to the DOM element DebugElement is an Angular2 class that contains all kinds of references and methods, to investigate an element or component Async() will allow the next test not to start until the async finishes all it's tasks. It tells to the test framework to wait until the return promise or observable is completed before treating the test as completed. fixture.detectChanges() : trigger change detection toBeTruthy() - Check if the value, when cast to a boolean, will be a truthy value i.e. Truthy values are all values that aren't 0, '' (empty string), false, null, NaN, undefinedor [] (empty array)*. To test if something evaluates to true, you use the toBeTruthy matcher: expect(true).toBeTruthy(); expect(12).toBeTruthy(); expect({}).toBeTruthy(); Likewise, to test if something evaluates to false, you use toBeFalsy: expect(false).toBeFalsy(); expect(null).toBeFalsy(); expect("").toBeFalsy(); WebPack developers need not to call compileComponents since it inlines templates and css as part of the automated build process that precedes running the test. Component = new componentclass() vs component-fixture-createinstance Isolated tests are for when you only want to test the internal behavior of the class. For instance class MyComponent { doSomething(): string {} } let component = new MyComponent(); expect(component.doSomething()).toBe('Hello World'); Here you're only going to test the behavior of the doSomething method. That's it. With the isolated tests you can't test the DOM interaction, as the template is never compiled. For this we should let Angular create the component. Then the component will go through through the actual lifecycle it would go through as it it was in the real application ng test --watch - To watch and re-run only the tests which have changed ng test --coverage - Free code coverage report