Testing the AngularJS Way

Our approach to testing AngularJS code is different than the approach we used when testing jQuery code. The main reason is that AngularJS is a framework whereas jQuery is a library. Your code typically calls into a library, which, in turn, does things for you. On the other hand, a framework grows around your code, to take control, and do things automagically without explicit calls; that needs some extra effort and changes how you test.

An example will help refresh the AngularJS way and put things in context before we start writing tests. Here’s a small controller that merely sets a greet variable’s value after converting a string to lowercase.

 var​ SampleController = ​function​($filter) ...

Get Test-Driving JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.