Created
September 14, 2016 08:53
-
-
Save psamsotha/2cce26fe4c9a3e73b8c8cbaa0fd738be to your computer and use it in GitHub Desktop.
This file contains 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 { Injectable } from '@angular/core'; | |
import { async, inject, TestBed } from '@angular/core/testing'; | |
import { MockBackend, MockConnection } from '@angular/http/testing'; | |
import { | |
Http, HttpModule, XHRBackend, ResponseOptions, | |
Response, BaseRequestOptions | |
} from '@angular/http'; | |
@Injectable() | |
class MetaManager {} | |
@Injectable() | |
class ApiConnectorService { | |
constructor(private _http: Http) {} | |
getHttp() { | |
return this._http; | |
} | |
} | |
@Injectable() | |
class EventDispatcher {} | |
@Injectable() | |
class ApiService { | |
constructor(private _metaManager: MetaManager, | |
private _connector: ApiConnectorService, | |
private _dispatcher: EventDispatcher) {} | |
getSomething(url) { | |
return this._connector.getHttp().get(url).map(res => res.text()); | |
} | |
} | |
describe('service: ApiService', () => { | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
providers: [ | |
{ | |
provide: Http, useFactory: (backend, options) => { | |
return new Http(backend, options); | |
}, | |
deps: [MockBackend, BaseRequestOptions] | |
}, | |
MockBackend, | |
BaseRequestOptions, | |
MetaManager, | |
ApiConnectorService, | |
EventDispatcher, | |
ApiService | |
] | |
}); | |
}); | |
it('should get value', | |
async(inject([ApiService, MockBackend], | |
(service: ApiService, backend: MockBackend) => { | |
backend.connections.subscribe((conn: MockConnection) => { | |
const options: ResponseOptions = new ResponseOptions({body: 'hello'}); | |
conn.mockRespond(new Response(options)); | |
}); | |
service.getSomething('http://dummy.com').subscribe(res => { | |
console.log('subcription called'); | |
expect(res).toEqual('hello'); | |
}); | |
}))); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment