我有一个简单的指令,并且在link函数中是一个$ broadcast receiver $ on。收到后,将调用范围函数:
return {
restrict: 'E',
link: function($scope, element, attrs) {
$scope.createCart = function () {
// Create cart
}
function getProductList() {
// get products lists
}
$scope.$on('create-shopping-cart', function() {
$scope.createCart();
getProductList();
});
}
};
我试过了:
describe('<-- myDirective Spec ------>', function () {
var scope, $compile, element, $httpBackend;
beforeEach(angular.mock.module('myApp'));
beforeEach(inject(function (_$rootScope_, _$compile_, _$httpBackend_) {
scope = _$rootScope_.$new();
$compile = _$compile_;
$httpBackend = _$httpBackend_;
var html = '<my-directive"></my-directive>';
element = $compile(angular.element(html))(scope);
spyOn(scope, '$on').and.callThrough();
scope.$digest();
}));
it('should be defined', function () {
expect(element).toBeDefined();
});
it('should broadcast ', function () {
scope.$broadcast('create-shopping-cart');
scope.$digest();
expect(element.isolateScope().createCart()).toHaveBeenCalled();
});
});
有了上述内容,我看到了错误:
TypeError: 'undefined' is not an object (evaluating 'element.isolateScope().createCart')
我如何测试$ scope.createCart(); AND getProductList();被称为?