'How to Angular unit test case for Subscribe for jasmine-karma?
I am trying to write test cases for array of subscription, when I tried to subscribe the getUserData method in the user service, it is giving me maximum call stack error.
It is not allowing me to call & subscribe to the data. can anyone please help me to write test cases for subscription.push.
user.component.ts:
import {Component, OnInit} from '@angular/core';
import {UserService} from "../services/user.service";
import { Subscription} from "rxjs";
@Component({
selector: 'user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
user;
subscription:Subscription[]=[];
constructor(private userService: UserService) {}
getData(){
this.subscription.push(
this.userService.getUserData().subscribe({
next: (data) => {
if (data.status === 'SUCCESS') {
this.user= data.user;
}
},
error: (error) => {
console.log(error);
},
})
);
}
}
user.component.spec.ts
import {UserComponent} from './user.component';
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {of} from 'rxjs';
import {UserService} from "../services/user.service";
describe('UserComponent', () => {
it(`should call getData method`,fakeAsync(()=>{
const spyOnData = spyOn(component.userService,'getUserData').and.returnValue(of(mockUserData));
component.getData();
tick();
expect(spyOnData ).toHaveBeenCalled();
}));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
