How to test an angular app with class inheritance?

 ̄綄美尐妖づ 提交于 2019-12-24 12:23:52

问题


I can test normal controllers fine. I cannot test controllers that inherit a base controller.

This is how we've been defining subclassed-controllers:

 app.NavController = app.BaseController.extend({
    ...
 });

This is the base:

app.BaseController = Class.extend({
    $scope: null,

    init: function($scope) {
        this.$scope = $scope;
        this.defineListeners();
        this.defineScope();
    },

    defineListeners: function() {
        // this.$scope.$on('$destroy',this.destroy.bind(this));
    },
    ...
});

app.controller('BaseController', ['$scope', function($scope) {
    return new app.BaseController($scope);
}]);

However, running Karma produces:

TypeError: 'undefined' is not an object (evaluating 'app.BaseController.extend')

Is there a different way of doing this? I've removed IIFE wrappers for testing. Class.js is included in my Karmaconfig. I'm using John Resig's Class inheritance.


回答1:


by adding this to instantiate the sub-classes prototype, we can get access to base class by simply injecting $controller as a dependency.

app.controller('SubController', ['$scope','$location','$controller',
        function($scope, $location, $controller) {
            var controller = {

...
controller.prototype = $controller('BaseController', {
                $scope: $scope
            });
            controller.init($location);
return controller;


来源:https://stackoverflow.com/questions/24045956/how-to-test-an-angular-app-with-class-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!