I have a simple component working well looking like that:
@Component({
selector: 'component',
template: `
{{userWithAsyncPipe | async | json}} <!-- = null in test -->
{{userFromOnInit | json}} <!-- working test -->
`,
})
export class Component implements OnInit {
constructor(private service: MyService) {
}
userWithAsyncPipe = this.service.getUser();
userFromOnInit;
ngOnInit() {
this.service.getUser().subscribe(user => this.userFromOnInit = user);
}
}
I'm trying to test it by mocking the service.getUser() method. So I wrote a test component approximatly looking like this:
describe('', () => {
...
let myService;
beforeEach(() => {
myService = TestBed.inject(MyService);
});
it('', () => {
spyOn(myService, 'getUser').and.returnValue(of({name: 'Jacob'}));
});
});
But when I look at the karma browser, only the variable userFromOnInit is resolved. userWithAsyncPipe is equal to null.
What the hell is that ?