Unit Test Complex Observed Flows

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

+4
1

, . Expect() mock Responses . , setTimeout() tick() .

:

if (req.method === RequestMethod.Get && req.url === 'api/getLine/data' && req.headers.get('range') === 'monthly') {
    setTimeout(() => { 
        connection.mockRespond(new Response(new ResponseOptions({ body: { data: 15 } })));      }, 100);
}

expect(value).toEqual(12);
tick(100);
expect(value).toEqual(15);

, , julia, , . flatMap() concatMap(), . - :

tick(100);
expect(values[0]).toEqual(12);
expect(values[1]).toEqual(15);
+2

Source: https://habr.com/ru/post/1676285/


All Articles