Skip to content

Instantly share code, notes, and snippets.

@geddski
Created June 19, 2013 16:40
Show Gist options
  • Save geddski/5815798 to your computer and use it in GitHub Desktop.
Save geddski/5815798 to your computer and use it in GitHub Desktop.
Demystification of Angular's services.
var expect = chai.expect;
describe('services', function(){
var goat, monkey, monkey2, Donkey, tiger1, tiger2, lion;
beforeEach(function(){
//load the module
module('app');
//configure providers
angular.module('app').config(function(TigerProvider){
TigerProvider.setScariness('high');
});
inject(function($injector) {
monkey = $injector.get('monkey');
monkey2 = $injector.get('monkey');
goat = $injector.get('goat');
Donkey = $injector.get('Donkey');
tiger1 = $injector.get('Tiger');
tiger2 = $injector.get('Tiger');
lion = $injector.get('Lion');
});
});
describe('#service', function(){
it('creates an instance of the constructor function', function(){
expect(monkey.name).to.equal('joe');
});
it('returns the same instance every time', function(){
expect(monkey.rand).to.equal(monkey2.rand);
});
});
describe('#factory', function(){
it('returns the result of invoking the function', function(){
expect(goat.name).to.equal('goat singleton');
});
describe('having that function return a constructor function', function(){
it('lets you create your own instances', function(){
var donkey = new Donkey('sam');
expect(donkey.name).to.equal('sam');
});
});
});
describe('#provider', function(){
describe('providers that are functions', function(){
it('creates a new instance of your function, and calls $get() on it, returning whatever $get returns', function(){
expect(tiger1.name).to.equal('tiger singleton');
});
it('returns the same instance every time', function(){
expect(tiger1.rand).to.equal(tiger2.rand);
});
it('calls $get only once', function(){
expect(tiger1.instanceRand).to.equal(tiger2.instanceRand);
});
});
describe('providers that are objects', function(){
it("invokes the object's $get method to get the instance", function(){
expect(lion.name).to.equal('lion singleton');
});
});
describe('configuring a provider', function(){
it('lets you set things up before any injection takes place', function(){
expect(tiger1.scariness).to.equal('high');
});
});
});
});
var app = angular.module('app', []);
//define a "class", will be instanciated
app.service('monkey', function(){
this.name = "joe";
this.rand = Math.random();
});
//can return an object (singleton)
app.factory('goat', function(){
return {
name: "goat singleton"
};
});
//can return a constructor function to use yourself
app.factory('Donkey', function(){
return function Donkey(name){
this.name = name;
};
});
//.factory is just a shortcut for $provide.provider(name, {$get: function()...})
//provider function gets new()'d then returns result of $get()
// new Tiger().$get()
app.provider('Tiger', function(){
var scariness = 'low';
var rand = Math.random();
this.setScariness = function(s){
scariness = s;
};
this.$get = function(){
return {
name: "tiger singleton",
rand: rand,
instanceRand: Math.random(),
scariness: scariness
};
};
});
app.provider('Lion', {
$get: function(){
return {
name: "lion singleton"
};
}
});
@geddski
Copy link
Author

geddski commented Jun 21, 2013

here's my karma.conf.js for running this:

files = [
  MOCHA,
  MOCHA_ADAPTER,
  'angular.js',
  'angular-mocks.js',
  'node_modules/chai/chai.js',
  'services/services.js',
  'services/services-test.js'
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment