Angularjs multiple services and Jasminejs unit testing a beginners guide

Angularjs using multiple services and Unit testing services with jasminejs

(update: 29/01/12) I have moved away from angular.module(‘ServiceName’, value( 
and instead use myServices.factory(‘ServiceName’, function(){ as I found  it did not do as I expected to.

Services are a bit harder to unit test and get configured than controllers.
Most examples have a single service or 2 services in a single file.
This is harder to maintain with source control as multiple people could be making changes on the same file.  Here is how to set up and use services in different files.

I have also included some example code on how to Unit test your angularjs service with jasperjs.


Add a new module do your app.js and then reference it inside your angular.module [] brackets.  The deceleration of the ‘’ allows you then to use this module in the services. 

var myServices = angular.module('', []);
var myApp = angular.module('myApp', [''])
  .config(['$routeProvider', function ($routeProvider) {
      .when('/', {
        templateUrl: 'views/my-controller.html',
        controller: 'MyCtrl'


Add 2 services to your index.html so they are included in your app.

  <script src="scripts/services/first-service.js" >
  <script src="scripts/services/second-service.js" >


Here is a example service containing one value and one function.

'use strict';

myServices.factory('FirstService', function(){
var firstService = {};
firstService.aValue = 'one'; = function (param1) {
    return this.aValue+ ' two';
return firstService;


Second service does nothing special just a value an a method also.

myServices.factory('SecondService', function(){
  var sercondService = {};
  secondService.animal = 'penguin';
  secondService.whereDoTheyLive = function () {
    return this.aValue+ ' live in the Antarctic NOT in the Arctic.  The Arctic is for the Polar Bears.';

  return secondService;

How to do it wrong




angular.module('',[]).value('SecondService', ... );

The problem with the above

The problem is the first-service is the first called by the inclusion in the index.html first.

You cannot add 2 or more services onto the where both use the [] notation.  
I found it is best to assign the in the /app/app.js and then just reference it in all the other services you are going to create.

Testing using Jasperjs


The below code will setup the first service for testing and importantly the inject will use the global service var as the service so we can run the methods and values upon it.

describe 'FirstService', ->
  service = {}
  beforeEach module('')

  beforeEach inject (FirstService) ->
    service = FirstService

  describe 'the first service', ->
    it 'should have a value',  ->


The generated javascript that will be run by the jasperjs.

// Generated by CoffeeScript 1.4.0
(function() {
  describe('FirstService', function() {
    var service;
    service = {};

beforeEach(inject(function(FirstService) {
      return service = FirstService;
    return describe('the first service', function() {
      it('should have a value', function() {


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s