My profilecomponent is designed to receive user data from a service in the form of an Object
public profiles$: Observable<IPerson>;
constructor(private router: Router, private userService: UserService) {}
ngOnInit(): void {
this.profiles$ = this.userService.getUser();
console.log(this.profiles$);
}
Now, I am trying to create a karma test to ensure that the data is being received properly. However, I encounter an error stating TypeError: Cannot read properties of undefined (reading 'and')
beforeEach(async () => {
const UserServiceSpy = jasmine.createSpyObj(['getUser']);
UserServiceSpy.getUser().and.returnValue();
await TestBed.configureTestingModule({
declarations: [ProfileComponent],
providers: [
{
provide: UserService,
useValue: UserServiceSpy,
},
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ProfileComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
It seems that the error is originating from this line of code: UserServiceSpy.getUser().and.returnValue();
However, I am confused as getUser already returns an observable. There should be no need for of()
getUser(): Observable<IPerson> {
let data: any = localStorage.getItem(this.storageID);
return of(JSON.parse(data));
}