相关疑难解决方法(0)

使用提供程序测试组件

我有一个用于服务隔离场景的服务 SoundPanelService(例如https://angular.io/guide/hierarchical-dependency-injection#scenario-service-isolation)

@Injectable()
export class SoundPanelService {
  recorded = new Subject<Sound>();

  constructor() {
  }
}
Run Code Online (Sandbox Code Playgroud)

我有 SoundPanelComponent

Component({
  selector: 'app-sound-panel',
  templateUrl: './sound-panel.component.html',
  styleUrls: ['./sound-panel.component.css'],
  providers: [SoundPanelService] // Service isolation 
})
export class SoundPanelComponent implements OnInit {
  recorded = new Subject<Sound>();

  constructor(private soundPanelService: SoundPanelService) {
    this.soundPanelService.recorded.subscribe((data) => {
      this.recorded.next(data);
    });
  }

  ngOnInit() {
  }

}
Run Code Online (Sandbox Code Playgroud)

sound-panel.component.html

<app-sound-player></app-sound-player>
<app-sound-recorder></app-sound-recorder>
Run Code Online (Sandbox Code Playgroud)

SoundPlayer 和 SoundRecorder 通过服务 SoundPanelService 与 soundpanel 通信。

我想测试 SoundPanelComponent

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By …
Run Code Online (Sandbox Code Playgroud)

jasmine karma-runner angular angular8

2
推荐指数
1
解决办法
317
查看次数

标签 统计

angular ×1

angular8 ×1

jasmine ×1

karma-runner ×1