我正在为具有异步服务的组件编写 angular 7 的单元测试用例并收到此错误:
错误:预期的 spy create 已被调用一次。它被调用了 0 次。
这是我的组件:
export class RegistrationComponent implements OnInit {
submitRegistrationForm() {
if (this.profileForm.invalid) {
this.validateAllFields(this.profileForm);
} else {
// send a http request to save this data
this.guestUserService.create(this.profileForm.value).subscribe(
result => {
if (result) {
console.log('result', result);
this.router.navigate(['/login']);
}
},
error => {
console.log('error', error);
});
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试用例:
describe('RegistrationComponent', () => {
let component: RegistrationComponent;
let fixture: ComponentFixture<RegistrationComponent>;
let myService;
let mySpy;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [RegistrationComponent], …Run Code Online (Sandbox Code Playgroud)