0

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.

Ivo Coumans
  • 759
  • 8
  • 23

1 Answers1

0

In my opinion this should work... But try calling the $controller() function with a second argument that is the scope, as seen in this example: How to test John papa vm.model unit testing with jasmine?

Maybe that has an influence on how the scope is created...

Community
  • 1
  • 1
ogugger
  • 122
  • 6