Created
August 6, 2014 18:22
-
-
Save cybersamx/107c3cbc3a846a471531 to your computer and use it in GitHub Desktop.
Inject a custom AngularJS controller object to a (Jasmine) unit test spec.
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
// 3 ways to inject custom AngularJS controller to a Jasmine test spec. | |
// Method 1 | |
describe('myNameSpace.controllers', function() { | |
describe('MyController', function() { | |
var scope, createController; | |
beforeEach(module('myNameSpace.controllers')); | |
beforeEach(inject(function($injector) { | |
var $rootScope = $injector.get('$rootScope'); | |
scope = $rootScope.$new(); | |
var $controller = $injector.get('$controller'); | |
createController = function() { | |
return $controller('MyController', { | |
'$scope': scope | |
}); | |
}; | |
})); | |
it('should be defined', function() { | |
var controller = createController(); | |
expect(controller).toBeTruthy(); | |
}); | |
}); | |
}); | |
// Method 2 | |
describe('myNameSpace.controllers', function() { | |
describe('MyController', function() { | |
var scope, createController; | |
beforeEach(module('myNameSpace.controllers')); | |
beforeEach(inject(function($controller) { | |
scope = {}; | |
createController = function() { | |
return $controller('MyController', { | |
'$scope': scope | |
}); | |
}; | |
})); | |
it('should be defined', function() { | |
var controller = createController(); | |
expect(controller).toBeTruthy(); | |
}); | |
}); | |
}); | |
// Method 3 (Shortest version) | |
describe('myNameSpace.controllers', function() { | |
describe('MyController', function() { | |
beforeEach(module('myNameSpace.controllers')); | |
it('should be defined', inject(function($controller) { | |
var scope = {}; | |
var controller = $controller('MyController', { | |
'$scope': scope | |
}); | |
expect(controller).toBeTruthy(); | |
})); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment