Last active
August 29, 2015 13:57
-
-
Save joshdover/9750007 to your computer and use it in GitHub Desktop.
angular unit test blog post
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('Controller: MyCtrl', function () { | |
beforeEach(module('MyApp')); | |
var scope; | |
// Initialize the controller and a mock scope | |
beforeEach(inject(function ($controller, $rootScope) { | |
scope = $rootScope.$new(); | |
startController = function() { | |
$controller('MyCtrl', { | |
$scope: scope | |
}); | |
}; | |
})); | |
it('assigns to someValue', function () { | |
startController(); | |
expect(scope.someValue).toBe(true); | |
}); | |
describe('some function', function() { | |
it('changes someValue', function() { | |
startContoller(); | |
scope.someFunction(); | |
expect(scope.someValue).toBe(false); | |
}); | |
}); | |
}); |
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('Service: MyDataService', function() { | |
beforeEach(module('MyApp')); | |
var service; | |
beforeEach(inject(function(MyDataService) { | |
service = MyDataService; | |
})); | |
afterEach(inject(function($httpBackend, $rootScope) { | |
// Force all of the http requests to respond. | |
$httpBackend.flush(); | |
// Force all of the promises to resolve. | |
// VERY IMPORTANT: If we do not resolve promises, none of the dependent | |
// expectations will be called, and the spec may pass inadvertantly. | |
$rootScope.$digest(); | |
// Check that we don't have anything else hanging. | |
$httpBackend.verifyNoOutstandingExpectation(); | |
$httpBackend.verifyNoOutstandingRequest(); | |
})); | |
it('creates a question', inject(function($httpBackend) { | |
var article = {title: 'my title', body: 'my body'}; | |
// This will fail the spec if this http request does not happen. | |
$httpBackend.expect('POST', baseUrl + '/article', article) | |
.respond({response: {id:1, title: 'my title', body: 'my body'}); | |
service.create(article).then(function(data) { | |
expect(data.id).toBeGreatherThan(0); | |
expect(data.title).toEqual(article.title); | |
}); | |
})); | |
}); |
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('Service: MyService', function () { | |
beforeEach(module('MyApp')); | |
var service; | |
beforeEach(inject(function(MyService) { | |
service = MyService; | |
})); | |
describe('action', function() { | |
it('calls a dependent service', inject(function(DependentService) { | |
spyOn(DependentService, 'method'); | |
service.action(); | |
expect(DependentService.method).toHaveBeenCalled(); | |
})); | |
}); | |
}); |
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
// Lives in test/mocks/services | |
angular.module('MyApp') | |
.service('MockUserData', function() { | |
return { | |
getUser: function() { | |
return { name: 'Fake User' }; | |
} | |
}; | |
}); | |
// The actual test | |
describe('Service: MyService', function () { | |
beforeEach(module('MyApp')); | |
var service; | |
beforeEach(inject(function(MyService) { | |
service = MyService; | |
// $provide will inject the mock to replace 'UserData' into any | |
// component that requires 'UserData'. | |
module(inject(function($provide, MockUserData) { | |
$provide.value('UserData', MockUserData) | |
})); | |
})); | |
describe('action', function() { | |
// If you want to spy on the service being mocked, make sure you | |
// are spying on the mock and not the real service. | |
it('gets the user', inject(function(MockUserData) { | |
spyOn(MockUserData, 'getUser').and.callThrough(); | |
service.action(); | |
expect(MockUserData.getUser).toHaveBeenCalled(); | |
})); | |
}); | |
}); |
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('Service: MyService', function () { | |
// Load the service's module. | |
beforeEach(module('MyApp')); | |
var service; | |
// Grab the service for convenience. | |
beforeEach(inject(function(MyService) { | |
service = MyService; | |
})); | |
it('says hello', function() { | |
expect(service.sayHello()).toEqual('hello'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment