Simplest AngularJS Service and a Jasmine unit test

The syntax for creating an AnguarJS service is rather alien to me as a Java developer, so I struggled to get this right, despite looking at and trying a number of different examples from different articles and howtos.

I think I’ve got this distilled to a simplest possible example now, so for future reference, this is a bare minimum service:

[code language=”javascript”]

angular.module(‘’, [])
.factory(‘exampleService’, function () {

var serviceImpl = {};

//methods on service here

return serviceImpl;


The syntax for implementing methods on the Service is like this (insert into where the comment is in the above example:


return {
exampleMethod: function (exampleParam1) {
var result;

//do something here

return result;


A simple Jasmine test for a Service looks like this:


describe(‘Example service’, function () {
var exampleService;

it("Should be something here", function () {

var $injector = angular.injector([ ‘’ ]);

exampleService = $injector.get( ‘exampleService’ );
var result = exampleService.exampleMethod(1);


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.