我正在为Component with async service用角度7编写单元测试用例,并得到此错误:
错误:预期的间谍创建已被调用一次。被称为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);
});
}
}
单元测试用例:
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,但仍然会给出相同的错误。
如何解决此错误?
谢谢!
答案 0 :(得分:2)
预期被调用的间谍创建不是错误,而是测试失败。
之所以发生这种情况,是因为您没有使用callThrough();要么在您的间谍上。
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();
})
));
答案 1 :(得分:2)
SpyOn将帮助您设置在测试中调用该函数时应如何反应。基本上,这是创建模拟的茉莉花方式。
在您的情况下,您已定义了在调用服务功能(即callThrough)时应执行的测试。问题在于,您还需要对服务函数(或调用服务方法的范围函数)进行操作,以触发将调用Through的SpyOn。
[
['id' => 1, 'name' => 'Spend', 'amount' => 10.52],
['id' => 2, 'name' => 'Spend', 'amount' => 10.55],
]
});
这是一个简单的测试,我们将模拟SpyOn对字符串的响应
it('load snapshot',function(){
//setup
spyOn(MyService, 'loadSomething').and.callThrough(); //statement 2
//act
//either call the scope function which uses the service
//$scope.yourServiceCallFunction();
//or call the service function directly
MyService.loadSomething(1); //this will callThrough