Last active
May 11, 2017 15:51
-
-
Save michaelchadwick/075c93cf838180542be82b7944d5d61c to your computer and use it in GitHub Desktop.
async service testing
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
// src/app/service.spec.ts | |
//import { Observable } from 'rxjs/Observable'; | |
import { Observable } from 'rxjs/Rx'; | |
import { TestBed, inject, async } from '@angular/core/testing'; | |
import { MockBackend, MockConnection } from '@angular/http/testing'; | |
import { Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod } from '@angular/http'; | |
import 'rxjs/add/observable/from'; | |
import 'rxjs/add/observable/throw'; | |
// my custom api service | |
import { MyService } from './myservice'; | |
let api: MyService = null; | |
let backend: MockBackend = null; | |
let mockResponse = null; | |
describe('MyService: /arg1/#', () => { | |
class MockSuccessMethodAHttp extends Http { | |
constructor(backend, options) { | |
super(backend, options) | |
} | |
get() { | |
return Observable.from([ | |
new Response( | |
new ResponseOptions({ | |
body: JSON.stringify(mockResponse) | |
}) | |
) | |
]); | |
} | |
} | |
class MockFailedMethodAHttp extends Http { | |
constructor(backend, options) { | |
super(backend, options) | |
} | |
get() { | |
return Observable.throw(new Error('error')); | |
} | |
} | |
let setupTestBed = function(httpMock, mr = {}) { | |
TestBed.configureTestingModule({ | |
providers: [ | |
MyService, | |
MockBackend, | |
BaseRequestOptions, | |
{ | |
provide: Http, | |
useFactory: (backend: MockBackend, options: BaseRequestOptions) => new httpMock(backend, options), | |
deps: [MockBackend, BaseRequestOptions] | |
} | |
] | |
}); | |
mockResponse = mr; | |
inject([Http, MockBackend], (http: Http, mb: MockBackend) => { | |
api = new MyService(http); | |
backend = mb; | |
} | |
)(); | |
}; | |
it(`should return a defined value via async`, async(() => { | |
setupTestBed(MockSuccessMethodAHttp); | |
api.methodA(0).then( | |
(data) => { | |
expect(data).toBeTruthy(); | |
} | |
); | |
})); | |
it(`should return a value for foo for existing arg1 1`, async(() => { | |
mockResponse = { | |
foo: "123", | |
bar: "blue", | |
baz: "haha" | |
}; | |
setupTestBed(MockSuccessMethodAHttp, mockResponse); | |
api.methodA(1).then( | |
(data) => { | |
expect(data.foo).toBe('123'); | |
} | |
); | |
})); | |
it(`should call handle error from the promise for non-positive arg1`, async(() => { | |
setupTestBed(MockFailedMethodAHttp); | |
spyOn(api, 'handleError'); | |
console.log('api.methodA(1)', api.methodA(1)); | |
console.log('api.methodA(-1)', api.methodA(-1)); | |
console.log('api.methodA(3.6)', api.methodA(3.6)); | |
api.methodA(-1).then((result) => { | |
expect(api.handleError).toHaveBeenCalled(); | |
}, (err) => { | |
console.log('fail error', err); | |
}); | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment