Testing 

Web Services 

with Mocha




@basarat     


Motivation

  • There is a lot that happens in the Back-end 
    • Store
      •  Do some action over HTTP -> Serialize
      • Goes to an event store -> queue
      • Handle an Event 
    • Read
      • Query over HTTP 
      • Read the data
      • Serialize it back -> Send to client

Consumption is all JavaSCript

We test client side code that uses the backend


  • Single test to rule them all 

  • Verifies the back-end is fit for purpose

  • True E2E for the backend

Why not Angular midway testing


Been there done that. 

 Didn't like: 

  • Speed (karma is slower then raw Node.js)
  • Difficult to write tests 
    • Why care about angular.module (codegen!)
    • scope.apply!

We love our Codegen

  • Modular services
    • Approvals
    • Assets
    • Catalogues
    • Photography
    • ....


  • All of these are Angular Services

Simplifying Codegen 

Abstract away $http 

  • Have a JavaScript (TypeScript) class that has 
"get, post, delete, put"

  • Gives us an opportunity to handle global errors
(404, 500)

  • Simplify client side URL generation

A better Get Action



Sample Generated Service


A service that depends on BaseHttpService

Abstract for Node.js 


Our front-end code remains unchanged. 

We just instrument it for Node.js 

Sample front end code: 

$injector



$injector



$http





$q


The reward



The reward


Better debugging



Fiddler


See whats going on


Summary

  • Mocha is fast, simple and you have all the power of Node.js

  • Frontend :
     Abstract away $http 

  • Mocha:
      Write your $http, $injector. Bring in cool stuff like $q 

  • Advantages: 
    • Run Speed
    • Developer happiness

Questions? 




@basarat

Testing Web Services 

By basarat

Testing Web Services 

  • 1,563