【发布时间】:2016-03-01 12:00:04
【问题描述】:
我正在尝试测试一个指令:
define(['require'], function (require){
'use strict';
var myDirective = function ($rootScope, MyService) {
return {
restrict: 'E',
templateUrl: 'app/banner/banner.html',
scope: {},
controller: [ '$scope', '$element', function ($scope, element) {
$scope.sendEmail = function(data) {
$scope.showConfirmation = false;
$rootScope.$broadcast('process-email', data);
};
}]
};
};
return ['$rootScope', 'MyService', myDirective];
});
banner.html
<ul>
<li><a href="javascript:;" ng-click="sendEmail('process')" class="email-item">Send mail</a></li>
<li><a href="javascript:;" ng-click="sendEmail('clarify')" class="email-item">Clarify mail</a></li>
</ul>
测试:
define(['require', 'angular-mocks'], function (require) {
'use strict';
var angular = require('angular');
describe('MyDirective Spec ->', function () {
var scope, $compile, element, $httpBackend;
beforeEach(angular.mock.module('MyApp'));
beforeEach(inject(function (_$rootScope_, _$httpBackend_) {
scope = _$rootScope_.$new();
$compile = _$compile_;
$httpBackend = _$httpBackend_;
var html = '<div my-directive></div>';
element = $compile(angular.element(html))(scope);
scope.$digest();
}));
it('should test sendEmail', function () {
spyOn(scope, 'sendEmail').and.callThrough();
element.find('.email-item').click();
expect(scope.sendEmail).toHaveBeenCalled();
});
});
});
我得到错误:
Error: sendEmail() method does not exist
【问题讨论】:
-
您的任何测试都通过了吗?
-
不,他们现在没有
标签: angularjs unit-testing angularjs-directive jasmine karma-jasmine