注入依赖和DOM更改 - 覆盖测试的组件

优质
小牛编辑
130浏览
2023-12-01
  • overrideComponent
  • overrideDirective
  • overridePipe
  1. import {MessageComponent} from './message.component';
  2. import { provide } from '@angular/core';
  3. import {
  4. async,
  5. inject,
  6. TestBed,
  7. } from '@angular/core/testing';
  8. describe('MessageComponent', () => {
  9. let fixture;
  10. beforeEach(() => {
  11. TestBed.configureTestingModule({
  12. declarations: [MessageComponent],
  13. providers: []
  14. });
  15. fixture = TestBed.overrideComponent(MessageComponent, {
  16. set: {
  17. template: '<span>{{message}}</span>'
  18. }})
  19. fixture.detectChanges();
  20. });
  21. it('should set the message', async(inject([], () => {
  22. fixture.componentInstance.setMessage('Test message');
  23. fixture.detectChanges();
  24. fixture.whenStable().then(() => {
  25. const compiled = fixture.debugElement.nativeElement;
  26. expect(compiled.querySelector('span').innerText).toEqual('Test message');
  27. });
  28. })));
  29. });