我有一个用于服务隔离场景的服务 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)