Created
May 15, 2015 17:40
-
-
Save johnmcase/d92d4992bdf9cbc7e36c to your computer and use it in GitHub Desktop.
Simple Angular Mocking Example
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
// In src/ directory | |
angular.module('services.configService', []).service('ConfigService', [function() { | |
return {'I': 'am not a mock'}; | |
}]); |
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
// In spec/ directory | |
angular.module('mocks', []).factory('ConfigServiceMock', [function() { | |
var self = this; | |
self.init = function(){}; | |
self.getValue = function(){}; | |
return self; | |
}]); |
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
module.exports = function(config) { | |
config.set({ | |
basePath: '.', | |
frameworks: ['jasmine'], | |
files: [ | |
{ | |
pattern: 'node_modules/angular/angular.js' | |
}, | |
{ | |
pattern: 'node_modules/angular-mocks/angular-mocks.js' | |
}, | |
{ | |
pattern: 'src/**/*.js' | |
}, | |
{ | |
pattern: 'spec/**/*.js' | |
}], | |
reporters: ['progress'], | |
browsers: ['PhantomJS'] | |
}); | |
}; |
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
// In src/ directory | |
angular.module('services.loginService', []).service('LoginService', ['ConfigService', function(ConfigService) { | |
return {injectedService: ConfigService}; | |
}]); |
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
// In spec/ directory | |
describe('LoginService tests', function () { | |
var LoginService; | |
describe('No mocks', function() { | |
beforeEach(module('services.loginService')); | |
beforeEach(module('services.configService')); | |
beforeEach(inject(function (_LoginService_) { | |
LoginService = _LoginService_; | |
})); | |
it('should have been given the real service', function() { | |
expect(LoginService.injectedService).toBeDefined('Something should have been injected'); | |
expect(LoginService.injectedService).toEqual({'I': 'am not a mock'}); | |
}); | |
}); | |
describe('Using spy object', function() { | |
var ConfigServiceMock; | |
beforeEach(module('services.configService', function($provide) { | |
ConfigServiceMock = jasmine.createSpyObj('ConfigServiceMock', ['init', 'getValue']); | |
$provide.value('ConfigService', ConfigServiceMock); | |
})); | |
beforeEach(module('services.loginService')); | |
beforeEach(inject(function (_LoginService_) { | |
LoginService = _LoginService_; | |
})); | |
it('should have been given the spy object', function() { | |
expect(ConfigServiceMock).toBeDefined('The mock should have been defined'); | |
expect(LoginService.injectedService).toBeDefined('Something should have been injected'); | |
expect(LoginService.injectedService).toBe(ConfigServiceMock, 'The thing injected should be the spy object'); | |
}); | |
}); | |
describe('Using externally defined mock', function() { | |
var ConfigServiceMock; | |
beforeEach(module('mocks')); | |
beforeEach(module('services.configService', function($provide) { | |
$provide.factory('ConfigService', function() {return ConfigServiceMock;}); | |
})); | |
beforeEach(module('services.loginService')); | |
beforeEach(inject(function (_ConfigServiceMock_) { | |
ConfigServiceMock = _ConfigServiceMock_; | |
})); | |
beforeEach(inject(function (_LoginService_) { | |
LoginService = _LoginService_; | |
})); | |
it('should have been given the mock', function() { | |
expect(ConfigServiceMock).toBeDefined('The mock should have been defined'); | |
expect(LoginService.injectedService).toBeDefined('Something should have been injected'); | |
expect(LoginService.injectedService).toBe(ConfigServiceMock, 'The thing injected should be the mock'); | |
}); | |
}); | |
}); |
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
{ | |
"private": "true", | |
"devDependencies": { | |
"angular": "^1.3.15", | |
"angular-mocks": "^1.3.15", | |
"jasmine-core": "^2.3.4", | |
"karma": "^0.12.31", | |
"karma-jasmine": "^0.3.5", | |
"karma-phantomjs-launcher": "^0.1.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment