1

我正在与秋田州经理一起工作。我有一些订阅我的查询的服务/类。我试图用 jasmine createSpyObj 将一些数据模拟到我的查询中。

这是我的服务(部分):

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

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

这是我的测试:

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();
    });
});

不知何故,我有测试错误说:

“无法读取未定义的属性‘订阅’”

我的测试基于官方秋田文档:https ://datorama.github.io/akita/docs/angular/tests 猜猜问题是:我试图测试服务,而不是组件。还尝试提供存根查询 - 它像这样工作,但对于每个测试我都需要其他数据......

4

1 回答 1

0

解决方案很简单。返回存根后,我需要获取我的逻辑服务。所以:

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

   logicService = TestBed.get(SomeLogicService);
   logicService.someMethod();
});
于 2020-07-06T15:04:37.473 回答