I have the following function that (I think) returns a composite observable:
test(cell) {
const observ1 = this.store.select('range').flatMap((val: IRange) => {return this.getCellOEE(val.value)});
const observ2 = this.getCellOEE(cell);
return Observable.merge(observ1, observ2);
}
I use this in angular 4 service and try unit test with jasmine and karma.
So, I'm a mockery store like this:
class mockStore {
select(): Observable<IRange> {
return Observable.of({value: 'daily', start: moment(), end: moment()}, {value: 'monthly', start: moment(), end: moment()})
}
}
and then I have the following test:
it('should update result on store change', fakeAsync(inject(
[MockBackend, OeeService],
(backend: MockBackend, s: OeeService) => {
const urls = [];
backend.connections.subscribe((connection: MockConnection) => {
const req: any = connection.request;
urls.push(req.url);
if (req.method === RequestMethod.Get && req.url === 'api/getLine/data' req.headers.get('range') === 'daily') {
connection.mockRespond(new Response(new ResponseOptions({ body: { data: 12 } })));
}
if (req.method === RequestMethod.Get && req.url === 'api/getLine/data' &&
req.headers.get('range') === 'monthly') {
connection.mockRespond(new Response(new ResponseOptions({ body: { data: 15 } })));
}
});
let value;
s.test('powders').subscribe(val => {
value = val;
})
tick();
expect(value).toEqual(12);
tick();
expect(value).toEqual(15);
})
));
I hope this is clear what I'm trying to achieve, so when the function is test()initially called, a direct HTTP call is made, but then my mocking repository should release the parameter change, map this to http with the new parameters, and then return a different value.
I am currently receiving the first gear expect(), but the second with an error:
Expected 12, containing 15
, , ? , ? , , TDD, , .
Update:
unit test