Posts

Showing posts from January, 2014

Angular.js Unit Test with Custom Service Example

(Updated 7/13/2014)

I've been working a lot with Angular.js lately, and there were a lot of unknowns for me when it came to unit testing my code. Below is an example that I hope will help other people who have questions about how to write unit tests for Angular.

I use Jasmine as the unit testing framework, and in the example below I'm testing controller named DocumentController which makes use of a service called AttachmentService. In the example below, we're testing the ability of the DocmentController to delete attachments, which it does with the help of AttachmentService.

I've commented it heavily in an attempt to be very clear as to what this code is doing. I hope this helps you. :)

//References to the JS files that are required by these tests and everything involved.
//The order matters!

/// <reference path="../jasmine/jasmine.js" />
/// <reference path="../jasmine/boot.js" />
/// <reference path="../angular.js" />
/// <…