Skip to content

Instantly share code, notes, and snippets.

@kylewhitaker
Created September 11, 2017 15:24
Show Gist options
  • Save kylewhitaker/8c701373de45a5ddfd0bad9454651fec to your computer and use it in GitHub Desktop.
Save kylewhitaker/8c701373de45a5ddfd0bad9454651fec to your computer and use it in GitHub Desktop.
Wrap your observable subscriptions inside a FakeAsync zone and make it Tick!
import { fakeAsync, tick } from '@angular/core/testing';
import { Observable } from 'rxjs/Observable';
import { SomeComponent } from './some.component';
describe('SomeComponent:', () => {
let component: SomeComponent;
let mockDepThree: any;
beforeEach(() => {
mockDepThree = jasmine.createSpyObj('DepThree', ['capitalize']);
component = new SomeComponent(mockDepThree);
});
describe('subscribeAndDoSomething', () => {
it('should call depThree.capitalize on the value emitted', fakeAsync(() => {
component.subscribeAndDoSomething();
tick();
expect(mockDepThree.capitalize).toHaveBeenCalledWith('jabroni');
}));
});
});
import { Observable } from 'rxjs';
import { DepThree } from './dependencies';
export class SomeComponent {
constructor(private depThree: DepThree) { }
subscribeAndDoSomething(): void {
Observable.of('jabroni').subscribe((value) => {
this.depThree.capitalize(value);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment