lundi 6 juillet 2020

Angular testing service with akita query - mock data

Im working with Akita state manager for angular. I have some service / class which is subscribing to one of my queries. Im trying to mock some data into my query with jasmine createSpyObj.

This is my service (part):

export class SomeService implements OnDestroy {
  private activeElements$ = this.someAkitaQuery.selectActive();

  constructor(
        private someAkitaQuery: SomeAkitaQuery,
    ) {
        this.activeElements$.subscribe(activeElement => {
      ...
    });
    }
}

This is my test:

import { TestBed, async, tick } from '@angular/core/testing';
import { SomeLogicService } from './somelogic.service';
import { SomeQueryService } from './some.service';
import { SomeQuery } from '../stores/order/some/some.query';
import { of } from 'rxjs';


describe('EnhancementPriceService', () => {
    let logicService: SomeLogicService;
    let someQuery: jasmine.SpyObj<SomeQuery>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            providers: [
                {
                    provide: SomeQueryService,
                    useValue: jasmine.createSpyObj('SomeQueryService', ['get'])
                },
                {
                    provide: SomeQuery,
                    useValue: jasmine.createSpyObj('SomeQuery', ['selectActive'])
                }
            ]
        });
    }));

    beforeEach(() => {
        someQuery = TestBed.get(SomeQuery);
        logicService = TestBed.get(SomeLogicService);
    });

    it('should return something', () => {
        SomeQuery.selectActive.and.returnValue(
            of([])
        );
        tick();
        logicService.someMethod();
    });
});

Somehow I have test error which saying:

"Cannot read property 'subscribe' of undefined"

I based my test on oficial akita docs: https://datorama.github.io/akita/docs/angular/tests Guess problem is: Im trying to test Service, not component. Also tried to provide stub query - it was working like this, but for each test i need other data...

Aucun commentaire:

Enregistrer un commentaire