Skip to content

Instantly share code, notes, and snippets.

@johnmcase
Created May 15, 2015 17:40
Show Gist options
  • Save johnmcase/d92d4992bdf9cbc7e36c to your computer and use it in GitHub Desktop.
Save johnmcase/d92d4992bdf9cbc7e36c to your computer and use it in GitHub Desktop.
Simple Angular Mocking Example
// In src/ directory
angular.module('services.configService', []).service('ConfigService', [function() {
return {'I': 'am not a mock'};
}]);
// In spec/ directory
angular.module('mocks', []).factory('ConfigServiceMock', [function() {
var self = this;
self.init = function(){};
self.getValue = function(){};
return self;
}]);
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']
});
};
// In src/ directory
angular.module('services.loginService', []).service('LoginService', ['ConfigService', function(ConfigService) {
return {injectedService: ConfigService};
}]);
// 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');
});
});
});
{
"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