Created
March 22, 2013 15:59
-
-
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?
This file contains hidden or 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
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'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.itaware.eu/2012/10/19/angularjs-unit-tests-and-end-to-end-tests/ use the module function in angular mocks