Itβs a little difficult to provide an accurate guide without seeing an example of live code (therefore, it is usually recommended to create a bar with a template for Jasmine tests), but it looks like your init method executes some installation logic, which should differ depending on the environment. If so, then moving forward would be to encapsulate this initialization logic in a dedicated service and make fun of this service during testing (this is exactly what @Joe Dyndale offers).
Assuming your controller looks like this:
app.controller('MainCtrl', function($scope) { $scope.init = function() {
It can be reorganized into:
app.factory('InitService', function() { return { init = function() {
and then a mock test might look like this:
describe('Testing an initializing controller', function() { var $scope, ctrl; //you need to indicate your module in a test beforeEach(module('plunker')); beforeEach(module(function($provide){ $provide.factory('InitService', function() { return { init: angular.noop }; }); })); beforeEach(inject(function($rootScope, $controller) { $scope = $rootScope.$new(); ctrl = $controller('MainCtrl', { $scope: $scope }); })); it('should test sth on a controller', function() { // }); });
Finally here is the living code in the plunker
pkozlowski.opensource
source share