我在制作茉莉花间谍时遇到了麻烦。角度1.5分量与电流的结合:&amp ;.我想检测何时调用notify函数中的分页函数。
测试
beforeEach(inject(function ($rootScope, _$componentController_) {
model = {
min: 1,
max: 10,
current: 1,
notify: function () {
return function paging(page){console.log(page);}
}
};
$componentController = _$componentController_;
}));
it('will not page forward on page 10', function () {
model.current = 10;
pager.pageForward();
expect(model.current).toBe(10);
});
组件摘要
model.pageFoward() {
model.notify()(model.current);
}
答案 0 :(得分:0)
在这种情况下,你不需要监视,你应该创建一个间谍并在model.notify
中返回。尝试:
var notifyReturnFunction;
...
notifyReturnFunction = jasmine.createSpy('notifyReturnFunction');
model = {
min: 1,
max: 10,
current: 1,
notify: function() {
return notifyReturnFunction;
}
};
...
现在你可以断言它已被调用:
it('will not page forward on page 10', function () {
model.current = 10;
pager.pageForward();
expect(model.current).toBe(10);
expect(notifyReturnFunction).toHaveBeenCalled();
// Or
expect(notifyReturnFunction).toHaveBeenCalled(model.current);
});