Last active
December 28, 2015 20:53
-
-
Save alexanderfrankel/c76995e93effc363c168 to your computer and use it in GitHub Desktop.
testing-in-karma-my-controller-spec.js
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
// contoller test | |
// spec/controllers/my-controller.js | |
describe('MyController', function() { | |
// define some variables | |
var scope, httpBackend, myController; | |
// load in our app | |
beforeEach(module('MyApp')); | |
// code to execute before each of our tests | |
beforeEach(inject(function($rootScope, $httpBackend, $controller) { | |
httpBackend = $httpBackend; | |
scope = $rootScope.$new(); | |
myController = $controller('MyController', {$scope: scope}); | |
// simulates a GET request to our server | |
httpBackend.when('GET', '/students') | |
.respond({students: [{name: "alex", grade: "third"}, | |
{name: "tom", grade: "second"}, | |
{name: "silvio", grade: "first"} | |
]}); | |
httpBackend.flush(); | |
})); | |
// code to execute after each of our tests | |
afterEach(function() { | |
httpBackend.verifyNoOutstandingExpectation(); | |
httpBackend.verifyNoOutstandingRequest(); | |
}); | |
// we can break each part of our controller down into smaller modules | |
describe('initialization', function() { | |
it('sets grades to default array of grades', function() { | |
expect($scope.grades).to.equal(["first", "second", "third"]); | |
}); | |
it('sets selectedGrade to null', function() { | |
expect($scope.selectedGrade).to.be.null; | |
}); | |
it('sets selectedStudents to empty array', function() { | |
expect($scope.selectedStudents).to.equal([]); | |
}); | |
it('sets allStudents to the index of students returned from the server', function() { | |
expect($scope.allStudents).to.equal([{name: "alex", grade: "third"}, | |
{name: "tom", grade: "second"}, | |
{name: "silvio", grade: "first"} | |
]); | |
}); | |
}); | |
describe('selecting a grade', function() { | |
beforeEach(function() { | |
$scope.selectGrade("third"); | |
}); | |
it('sets selectedGrade to the grade that was selected', function() { | |
expect($scope.selectedGrade).to.equal("third"); | |
}); | |
it('sets selectedStudents to an array all students in the grade that was selected', function() { | |
expect($scope.selectedStudents).to.equal([{name:"alex", grade:"third"}]); | |
}); | |
}); | |
describe('deselecting a grade', function() { | |
beforeEach(function() { | |
$scope.deselectGrade(); | |
}); | |
it('resets selectedGrade to null', function() { | |
expect($scope.selectedGrade).to.be(null); | |
}); | |
it('resets selectedStudents to empty array', function() { | |
expect($scope.selectedStudents).to.equal([]); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment