4
的情况是我有一个ChildCtrl
控制器从BaseCtrl
继承以下this inheritance pattern:
angular.module('my-module', [])
.controller('BaseCtrl', function ($scope, frobnicate) {
console.log('BaseCtrl instantiated');
$scope.foo = frobnicate();
// do a bunch of stuff
})
.controller('ChildCtrl', function ($controller, $scope) {
$controller('BaseCtrl', {
$scope: $scope,
frobnicate: function() {
return 123;
}
});
});
假设BaseCtrl
做了一堆东西,已经是很好的测试,我想测试ChildCtrl
与某些参数实例化BaseCtrl
。我最初的想法是这些方针的东西:
describe("ChildCtrl", function() {
var BaseCtrl;
beforeEach(module('my-module'));
beforeEach(module(function($provide) {
BaseCtrl = jasmine.createSpy();
$provide.value('BaseCtrl', BaseCtrl);
}));
it("inherits from BaseCtrl", inject(function ($controller, $rootScope) {
$controller('ChildCtrl', { $scope: $rootScope.$new() });
expect(BaseCtrl).toHaveBeenCalled();
}));
});
然而,当我运行测试的谍照,不会被调用控制台显示“BaseCtrl实例化”,表明我的实例是$controller
使用的实际控制人,而不是提供$provide.value()
。
什么是最好的测试方法?