Last active
October 10, 2018 15:25
-
-
Save LayZeeDK/0f8510cb53a4573fa209bc3700d221c3 to your computer and use it in GitHub Desktop.
Heroes: Setting up a HeroService stub for testing the container component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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