问题
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