Scotch.io - Unit Testing in AngularJS

    Adam Morgan

Lead Frontend Developer @SeraphGroup

The Test Tooling Landscape

Karma

Jasmine

Protractor

Mocha

AVA

Chai

Cypress

Nightwatch

Why test?

  • Verify our code behaves as we expect it to
  • Avoid software defects
  • Manual QA is expensive and time-consuming

How and what should I test?

4!

1!

2!

Testing Adder with Jasmine

Test specs read like plain English

Karma Installation

Testing a factory (Hello, world)

Implementing the factory

Returning an array of users

  • Hard-coding users?!
    • Tests are about expectations - not "realness" of data
  • A coworker updates the service with another user
    • The code change was in the wrong service
    • A new founding member was added to the team (a change in requirements)

This is why developers often refer to test cases and their expectations for how our code should behave as documentation, or a design aid, for their actual code.

Finding a single user

Testing a controller

Testing a factory with $http

Success/Resolve

Testing a factory with $http

Error/Reject

Unit Testing in AngularJS

By atom-morgan

Unit Testing in AngularJS

  • 547