gpt4 book ai didi

javascript - 错误: Expected spy create to have been called

转载 作者:行者123 更新时间:2023-11-28 14:17:50 24 4
gpt4 key购买 nike

我正在 Angular 7 中为 Component with async service 编写单元测试用例并收到此错误:

Error: Expected spy create to have been called once. It was called 0times.

这是我的组件:

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);
});
}
}

单元测试用例:

  describe('RegistrationComponent', () => {
let component: RegistrationComponent;
let fixture: ComponentFixture<RegistrationComponent>;
let myService;
let mySpy;

beforeEach(async(() => {

TestBed.configureTestingModule({
declarations: [RegistrationComponent],
imports: [ ],
providers: [
{ provide: GuestUserService, useValue: new MyServiceStub() }]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(RegistrationComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should submit Registration Form', async(inject([Router], (router) => {
myService = TestBed.get(GuestUserService);
mySpy = spyOn(myService, 'create');
spyOn(router, 'navigate');
spyOn(component, 'submitRegistrationForm');


component.profileForm.controls['firstName'].setValue('Arjun');
component.profileForm.controls['lastName'].setValue('Singh');
component.profileForm.controls['password'].setValue('12345678');
component.profileForm.controls['confirmPassword'].setValue('12345678');
component.submitRegistrationForm();

expect(component.profileForm.invalid).toBe(false);

expect(component.submitRegistrationForm).toHaveBeenCalled();

expect(myService).toBeDefined();
expect(mySpy).toBeDefined();
expect(mySpy).toHaveBeenCalledTimes(1); // Getting error is this
expect(router.navigate).toHaveBeenCalled();
})
));

我尝试在 beforeEach 中移动 spy 减速,但仍然给出相同的错误。

如何修复这个错误?

谢谢!

最佳答案

预期已调用的 spy 创建不是错误,而是失败的测试。

发生这种情况是因为您没有使用 callThrough();也可以在你的 spy 上。

 it('should submit Registration Form', async(inject([Router], (router) => {

myService = TestBed.get(GuestUserService);
mySpy = spyOn(myService, 'create').and.callThrough(); //callThrough()

spyOn(router, 'navigate');

spyOn(component, 'submitRegistrationForm').and.callThrough(); //callThrough()


component.submitRegistrationForm();

expect(component.profileForm.invalid).toBe(false);

expect(component.submitRegistrationForm).toHaveBeenCalled();

expect(myService).toBeDefined();
expect(mySpy).toBeDefined();
expect(mySpy).toHaveBeenCalledTimes(1);
expect(router.navigate).toHaveBeenCalled();
})
));

关于javascript - 错误: Expected spy create to have been called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56652121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com