Skip to content

Instantly share code, notes, and snippets.

@LayZeeDK
Last active October 10, 2018 15:25
Show Gist options
  • Save LayZeeDK/0f8510cb53a4573fa209bc3700d221c3 to your computer and use it in GitHub Desktop.
Save LayZeeDK/0f8510cb53a4573fa209bc3700d221c3 to your computer and use it in GitHub Desktop.
Heroes: Setting up a HeroService stub for testing the container component
import { asapScheduler, of as observableOf } from 'rxjs';
import { femaleMarvelHeroes } from '../../test/female-marvel-heroes';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
import { HeroesContainerComponent } from './heroes.container';
describe(HeroesContainerComponent.name, () => {
function createHeroServiceStub(): jasmine.SpyObj<HeroService> {
const stub: jasmine.SpyObj<HeroService> = jasmine.createSpyObj(
HeroService.name,
[
'addHero',
'deleteHero',
'getHeroes',
]);
resetHeroServiceStub(stub);
return stub;
}
function resetHeroServiceStub(stub: jasmine.SpyObj<HeroService>): void {
stub.addHero
.and.callFake(({ name }: Partial<Hero>) => observableOf({
id: 42,
name,
}, asapScheduler))
.calls.reset();
stub.deleteHero
.and.callFake((hero: Hero) => observableOf(hero, asapScheduler))
.calls.reset();
stub.getHeroes
.and.returnValue(observableOf(femaleMarvelHeroes, asapScheduler))
.calls.reset();
}
const heroServiceStub: jasmine.SpyObj<HeroService> = createHeroServiceStub();
afterEach(() => {
resetHeroServiceStub(heroServiceStub);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment