Angular Testing: Jogesh K. Muppala
Angular Testing: Jogesh K. Muppala
Testing
Jogesh K. Muppala
Testing Angular Applications
• Angular was built from the ground-up to
facilitate testing
– Modular implementation with components,
services and providers etc.
– Dependency injection
2
Test-Driven Development
• Write an automated test case defining the
desired functionality
• Write application code to pass the test
• Refactor the code to meet coding standards
https://en.wikipedia.org/wiki/Test-driven_development
3
Unit Testing
• Testing individual units of code
– Restricted to verifying if the component’s logic is
implemented correctly
• Isolating the unit under test from other dependencies
is important
– Dependency injection enables us to inject mock
dependencies
– Clear separation in Angular between the DOM and logic
4
Jasmine
• Behavior driven development framework for
JavaScript
– Adopted to test Angular applications
– Use “describe” function to group our tests
– Use “it” function to define individual tests
5
Karma
• JavaScript based command line tool (NodeJS
application)
– Spawns a web server to load your application’s
source code
– Executes your tests in the browser
6
Angular Testing Utilities
• Create a test environment for the Angular application code under test
• Use them to condition and control parts of the application as they
interact within the Angular environment
• TestBed creates an Angular testing Module (@NgModule) to enable
testing of components
– TestBed.createComponent creates an instance of the component under test
– Gives access to ComponentFixture, a handle on the test environment
surrounding the created component.
– The fixture provides access to the component instance itself and to
the DebugElement, which is a handle on the component's DOM element.