gpt4 book ai didi

angularjs - 如何测试具有类继承的 Angular 应用程序?

转载 作者:行者123 更新时间:2023-11-28 20:43:17 27 4
gpt4 key购买 nike

我可以很好地测试普通 Controller 。我无法测试继承基本 Controller 的 Controller 。

这就是我们定义子类 Controller 的方式:

 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);
}]);

但是,运行 Karma 会产生:

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

有没有不同的方法呢?我已经删除了 IIFE 包装器以进行测试。 Class.js 包含在我的 Karmaconfig 中。我正在使用 John Resig 的类继承。

最佳答案

通过添加它来实例化子类原型(prototype),我们可以通过简单地注入(inject) $controller 作为依赖来访问基类。

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

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

关于angularjs - 如何测试具有类继承的 Angular 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24045956/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com