Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  195] [ 5]  / answers: 1 / hits: 50540  / 5 Years ago, tue, june 18, 2019, 12:00:00

I'm writing unit test cases in angular 7 for a Component with async service and getting this error:



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



Here is my Component:


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

Unit test case:


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

I tried to move the spy deceleration in beforeEach but still its giving the same error.


how to fix this error?


Thanks!


More From » angular

 Answers
311

Expected spy create to have been called is not an Error but a failed test.



This is happening because you haven't use callThrough(); on your spyOn either.



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

[#51986] Tuesday, June 11, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;