Last active
August 29, 2015 13:56
-
-
Save russmatney/8965310 to your computer and use it in GitHub Desktop.
Angular Controller Unit tests, featuring a mocked service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
describe('Controller: ScenesCtrl', function(){ | |
beforeEach(module('app')); | |
var scope, scenesCtrl; | |
var actId = '987'; | |
var listOfScenes = [ | |
{_id: '123', name:'scene one'}, | |
{_id: '456', name:'scene two'} | |
]; | |
beforeEach(inject(function($rootScope, $controller, $q){ | |
scope = $rootScope.$new(); | |
var sceneSrvc = { | |
create: function(scene){ | |
var deferred = $q.defer(); | |
deferred.resolve(scene); | |
return deferred.promise; | |
}, | |
save: function(scene){ | |
var deferred = $q.defer(); | |
deferred.resolve(scene); | |
return deferred.promise; | |
}, | |
list: function(){ | |
var deferred = $q.defer(); | |
deferred.resolve(listOfScenes); | |
return deferred.promise; | |
}, | |
delete: function(){ | |
var deferred = $q.defer(); | |
deferred.resolve(); | |
return deferred.promise; | |
} | |
}; | |
scenesCtrl = $controller('ScenesCtrl', {$scope: scope, SceneSrvc: sceneSrvc}); | |
//Potentially refactor to use RouteParams instead of scope | |
scope.actId = actId; | |
})); | |
it('should exist', function(){ | |
expect(scenesCtrl).not.toBe(null); | |
}); | |
it('should list scenes via act_id on load', function() { | |
scope.$digest(); | |
expect(scope.scenes).toEqual(listOfScenes); | |
}); | |
it('should create a new scene', function() { | |
scope.$digest(); | |
var newScene = {name:'new Scene'}; | |
scope.createScene(newScene); | |
scope.$digest(); | |
expect(scope.scenes).toContain(newScene); | |
/*jshint camelcase: false */ | |
expect(scope.scenes[scope.scenes.indexOf(newScene)].act_id).toEqual(actId); | |
}); | |
it('should update a modified scene', function() { | |
scope.$digest(); | |
var length = scope.scenes.length; | |
scope.scenes[0].name = 'new name'; | |
scope.saveScene(scope.scenes[0]); | |
scope.$digest(); | |
expect(scope.scenes.length).toBe(length); | |
//TODO: could be a better test for this | |
}); | |
it('should delete a scene', function(){ | |
scope.$digest(); | |
var length = scope.scenes.length; | |
scope.deleteScene(scope.scenes[0]); | |
scope.$digest(); | |
expect(scope.scenes.length).toBe(length - 1); | |
//TODO: could be a better test for this | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach module ($provide) -> | |
$provide.value 'ConfigSrvc', | |
get: -> 'http://api.app.com' | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment