2

Im currently involved in a project that has used typescript to build its single page anuglar app. Unfortunately they have done no unit testing. Being new to using typescript i am having problems getting any testing going. An example test is below. followed by the typescript file

When i run the test it fails to find the controller !

(function() {
'use strict';

    describe('CarController', function() {

        // Load the controllers module
        beforeEach(module('CarDealerApp'));

        var scope,
            CarController ;

        beforeEach(inject(function($controller) {
            scope = {};

            CarController = $controller('CarController', {
                $scope: scope
            });
        }));

        it('should set the car name', function() {

            expect(scope.carMake).toEqual('Ford');

        });

    });

})();


module CarDealerApp.Cars.Controllers {
    "use strict";

    export interface ICarScope extends angular.Scope {
        carName : string;
    }

    export class CarController {

        carName:string = "Ford";

        // $inject annotation
        public static $inject = [
            '$scope'
        ]

        constructor(private $scope: ICarScope){

            $scope.vm = this;
        }

    }
}
1
  • wouldn't you want to expect scope.vm.carMake or can you just do CarController.carMake? Commented Feb 22, 2015 at 20:34

2 Answers 2

4

Although what you have written there works, I suggest constructing your TypeScript controller (in your test) like so:

CarController = new CarDealerApp.Cars.Controllers.CarController($rootScope.$new());

Which is a cleaner construction, and is the proper way to do it when you're writing your TypeScript tests.

Sign up to request clarification or add additional context in comments.

Comments

1

Finally got this working.

Basically I needed to pass in the full module name for the controller like this:

CarController = $controller('CarDealerApp.Cars.Controllers.CarController');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.