Skip to content

Instantly share code, notes, and snippets.

@psamsotha
Created September 14, 2016 08:53
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 psamsotha/2cce26fe4c9a3e73b8c8cbaa0fd738be to your computer and use it in GitHub Desktop.
Save psamsotha/2cce26fe4c9a3e73b8c8cbaa0fd738be to your computer and use it in GitHub Desktop.
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