I'm trying to run Jasmine tests on my Angular app using Chutzpah in Visual Studio.
I have a controller that is defined as:
angular.module('testApp')
.controller('TestAppCtrl', ['TestService',
function ($service) {
var vm = this;
// just a function for testing
vm.sum = function (x, y) { return x + y };
}
]);
I then describe my Jasmine test as:
describe('TestAppCtrl', function () {
var ctrl, TestService;
beforeEach(module('testApp'));
beforeEach(inject(function ($controller, _TestService_) {
TestService= _TestService_;
ctrl = $controller('TestAppCtrl');
}));
it('ctrl is defined', function () {
expect(ctrl).toBeDefined();
});
it('1 + 2 equals 3', function () {
expect(ctrl).toBeDefined();
expect(ctrl.sum(1, 2)).toBe(3);
});
});
The second test, 1 + 2 equals 3
, fails. I get this error:
Message: TypeError: undefined is not a constructor (evaluating 'ctrl.sum(1, 2)') in file:///...
I've tried this in varying ways: defining a simple variable on the controller (vm.testVar = 12345
), or even by injecting the $scope
and defining variables on that. I correctly injected it on the test as well. Both of these attempts failed in the same error; it's as though the test runner doesn't recognize any values on the controller, even though the controller itself is defined.