如何测试与类继承的角度应用程序? [英] How to test an angular app with class inheritance?

查看:178
本文介绍了如何测试与类继承的角度应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以测试正常控制器的罚款。我不能测试继承基控制器控制器。

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({
    ...
 });

这是基础:

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')

是否有这样做的不同的方式?我已经去IIFE包装进行测试。 Class.js包括在我的Karmaconfig。我使用的是约翰Resig的类继承。

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.

推荐答案

通过将本实例的子类的原型,我们可以通过简单地注入$控制器作为一个依赖可以访问基类。

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;

这篇关于如何测试与类继承的角度应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆