以下是代码:
// component
export class MockDateComponent implements OnInit {
date: number;
// ...
initDate() {
this.date = new Date().getTime()
}
}
// spec
describe('...', () => {
// ...
it('should init date with current timestamp', () => {
jasmine.clock().mockDate(new Date(1546263001000));
component.initDate();
expect(component.date).toBe(1546263001000);
});
})
以上代码将通过。
但是当我将下面的代码添加到spec
文件中时,它会抛出错误Error: Jasmine Clock was unable to install over custom global timer functions. Is the clock already installed?
,我不知道为什么。
describe('...', () => {
// ...
beforeEach(() => {
// ...
jasmine.clock().install();
});
afterEach(() => {
jasmine.clock().uninstall();
});
it('should init date with current timestamp', () => {
// codes inner are the same
});
})
该项目是由Angular Cli创建的,以下是版本信息:
@angular/common v6.1.10`
Angular CLI: 6.0.3
Node: 11.1.0
OS: darwin x64
谢谢您的回答!