【发布时间】:2016-08-11 21:44:57
【问题描述】:
假设我有一个类似的指令:
.directive('myDir', function(TemplateHandler){
return {
...
controller: function(ExploreCmd){
this.foo = {
bar: function(){...}
};
this.foo.bar();
}
}
});
我想测试一下,当指令被加载时,this.foo.bar() 函数已经被调用,我该如何实现呢?
我试过了:
beforeEach(inject(function($compile, $rootScope){
scope = $rootScope.$new();
element = angular.element('<js-shell></js-shell>');
$compile(element)(scope);
scope.$digest();
isolatedScope = element.isolateScope().vm;
spyOn(isolatedScope.foo, 'bar');
}));
it('should register the explore command', () => {
expect(isolatedScope.foo.bar).toHaveBeenCalled();
});
但问题是spyOn(isolatedScope.foo, 'bar');是在isolatedScope.foo.bar被调用之后被调用的,所以测试失败了。
【问题讨论】:
标签: angularjs testing angularjs-directive jasmine spy