Skip to content

Instantly share code, notes, and snippets.

@hcabnettek
Created March 22, 2013 15:59
Show Gist options
  • Save hcabnettek/5222429 to your computer and use it in GitHub Desktop.
Save hcabnettek/5222429 to your computer and use it in GitHub Desktop.
How do I get a reference to the angularjs controller defined with the 'controller' function? The test fails saying 'TestCtrl' is not a function. I have tried both with and without the quotes. $controller doc says it takes a constructor function or a string as an argument. Can anyone help?
describe('controller tests', function () {
var m, c;
beforeEach(function () {
m = angular.module('foo', []);
c = m.controller('TestCtrl', ['$scope', function ($scope) {
$scope.data = { foo: 'bar' };
$scope.changeFoo = function () {
$scope.data.foo = 'baz';
};
}]);
});
afterEach(function () {
});
describe('HomeCtrl controller ', function () {
var scope, ctrl;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('TestCtrl', { $scope: scope });
}));
it('scope.data should be defined', function () {
expect(scope.data).toBeDefined();
expect(scope.data.foo).toBe('bar');
expect(scope.changeFoo).toBeDefined();
scope.changeFoo();
expect(scope.data.foo).toBe('baz');
});
});
});
@hcabnettek
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment