Skip to content

Instantly share code, notes, and snippets.

@clemcke
Last active February 8, 2016 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clemcke/c5902028a1b0934b03d9 to your computer and use it in GitHub Desktop.
Save clemcke/c5902028a1b0934b03d9 to your computer and use it in GitHub Desktop.
Sample spec that uses appInjector() and a service
import {it, describe, expect, inject, beforeEach, beforeEachProviders} from 'angular2/testing'
import {PersonService, PERSON_SERVICE_PROVIDERS, personServiceLoadedPromise} from './person.service'
// Contents of person.service.ts
//@Injectable()
//export class PersonService {
// constructor(private _http: Http) { }
//
// init(): Promise<boolean>{
// return this._http.get('myurl').toPromise();
// }
//}
//
//function personServiceLoadedPromise(next, prev): Promise<boolean>{
// return appInjector().get(PersonService).init();
//}
//
//const PERSON_SERVICE_PROVIDERS = [
// PersonService,
// HTTP_PROVIDERS
//];
import {appInjector} from "./app-injector";
import {Injector} from 'angular2/core'
describe('PersonService',()=>{
describe('#personServiceLoadedPromise',()=> {
beforeEachProviders(()=>[PERSON_SERVICE_PROVIDERS]);
beforeEach(inject([PersonService],()=>{
appInjector(Injector.resolveAndCreate([PERSON_SERVICE_PROVIDERS]));
}));
it('calls #init()', ()=> {
let service = appInjector().get(PersonService);
spyOn(service, 'init');
personServiceLoadedPromise(null,null);
expect(service.init).toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment