Created
December 6, 2013 03:12
-
-
Save congjf/7817982 to your computer and use it in GitHub Desktop.
AngularJS Controller Testing
This file contains hidden or 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
AngularJS Controller Testing |
This file contains hidden or 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
var myApp = angular.module('myApp',[]); | |
myApp.controller('MyController', function($scope) { | |
$scope.spices = [{"name":"pasilla", "spiciness":"mild"}, | |
{"name":"jalapeno", "spiceiness":"hot hot hot!"}, | |
{"name":"habanero", "spiceness":"LAVA HOT!!"}]; | |
$scope.spice = "habanero"; | |
}); |
This file contains hidden or 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
describe('myController function', function() { | |
describe('myController', function() { | |
var $scope; | |
beforeEach(module('myApp')); | |
beforeEach(inject(function($rootScope, $controller) { | |
$scope = $rootScope.$new(); | |
$controller('MyController', {$scope: $scope}); | |
})); | |
it('should create "spices" model with 3 spices', function() { | |
expect($scope.spices.length).toBe(3); | |
}); | |
it('should set the default value of spice', function() { | |
expect($scope.spice).toBe('habanero'); | |
}); | |
}); | |
}); |
This file contains hidden or 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
describe('state', function() { | |
var mainScope, childScope, grandChildScope; | |
beforeEach(module('myApp')); | |
beforeEach(inject(function($rootScope, $controller) { | |
mainScope = $rootScope.$new(); | |
$controller('MainCtrl', {$scope: mainScope}); | |
childScope = mainScope.$new(); | |
$controller('ChildCtrl', {$scope: childScope}); | |
grandChildScope = childScope.$new(); | |
$controller('GrandChildCtrl', {$scope: grandChildScope}); | |
})); | |
it('should have over and selected', function() { | |
expect(mainScope.timeOfDay).toBe('morning'); | |
expect(mainScope.name).toBe('Nikki'); | |
expect(childScope.timeOfDay).toBe('morning'); | |
expect(childScope.name).toBe('Mattie'); | |
expect(grandChildScope.timeOfDay).toBe('evening'); | |
expect(grandChildScope.name).toBe('Gingerbreak Baby'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment