некоторые ошибки о часах жасмина в модульном тесте Angular - PullRequest
0 голосов
/ 01 января 2019

Вот коды:

// 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

Спасибо за ваш ответ!

...