测试组件 - 测试异步操作

优质
小牛编辑
118浏览
2023-12-01
  1. import { QuoteComponent } from './quote.component';
  2. import { provide } from '@angular/core';
  3. import {
  4. async,
  5. TestBed,
  6. fakeAsync,
  7. tick,
  8. } from '@angular/core/testing';
  9. class MockQuoteService {
  10. public quote: string = 'Test quote';
  11. }
  12. }
  13. describe('Testing Quote Component', () => {
  14. let fixture;
  15. beforeEach(() => {
  16. TestBed.configureTestingModule({
  17. declarations: [
  18. QuoteComponent
  19. ],
  20. ]
  21. });
  22. fixture = TestBed.createComponent(QuoteComponent);
  23. fixture.detectChanges();
  24. });
  25. it('Should get quote', fakeAsync(() => {
  26. fixture.componentInstance.getQuote();
  27. tick();
  28. fixture.detectChanges();
  29. const compiled = fixture.debugElement.nativeElement;
  30. expect(compiled.querySelector('div').innerText).toEqual('Test quote');