简体   繁体   English

如何测试从服务接收 Observable 的属性值

[英]How to test property values receiving Observable from service

I would like to ask what is the common practice of testing properties which receives data from services inline.我想问一下测试从内联服务接收数据的属性的常见做法是什么。

I am not able to test my property which is being receiving values from service - defined inline.我无法测试从服务定义的内联值接收值的属性。 here is my property这是我的财产

    readonly filters$ = forkJoin({
        institutions: this.dataExtractService.getInstitutions(),
        dataSetTypes: this.dataExtractService.getDataSetTypes()
    });

getInstitutions() and getDataSetTypes() return Observable<string()> getInstitutions()getDataSetTypes()返回Observable<string()>

Here is my spec这是我的规格

    let component: ExtractRetrievalComponent;
    let fixture: ComponentFixture<ExtractRetrievalComponent>;
    let formBuilder: FormBuilder;
    let dataExtractService: DataExtractService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            schemas: [NO_ERRORS_SCHEMA],
            imports: [HttpClientTestingModule],
            declarations: [ExtractRetrievalComponent],
            providers: [
                FormBuilder, DataExtractService
            ]
        }).compileComponents();
        fixture = TestBed.createComponent(ExtractRetrievalComponent);
        component = fixture.componentInstance;

        formBuilder = TestBed.inject(FormBuilder);
        dataExtractService = TestBed.inject(DataExtractService);
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });

    it('should get filters', (done) => {
        const expectedInstitution = ['expectedInstitutions'] as string[];
        const expectedDataSetType = ['expectedDataSetType'] as string[];
        const expectedFilter = {
            institutions: expectedInstitution,
            dataSetTypes: expectedDataSetType
        };
        spyOn(component.filters$, 'subscribe').and.callThrough();

        spyOn(dataExtractService, 'getInstitutions').and.returnValue(of(expectedInstitution));
        spyOn(dataExtractService, 'getDataSetTypes').and.returnValue(of(expectedDataSetType));
        fixture.detectChanges();

        component.filters$.subscribe(result => {
            expect(result).toEqual(expectedFilter);
            done();
        });
    });

test is failing with result测试失败,结果

Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

I have also incerased the timeout interval with no luck.我也增加了超时间隔,但没有运气。 Any hints?有什么提示吗?

Since the filters$ is an instance variable, the following lines are happening too late:由于filters$是一个实例变量,因此以下几行发生得太晚了:

spyOn(dataExtractService, 'getInstitutions').and.returnValue(of(expectedInstitution));
spyOn(dataExtractService, 'getDataSetTypes').and.returnValue(of(expectedDataSetType));

The first fixture.detectChanges() calls ngOnInit and I think createComponent creates the class and populates the instance variables.第一个 fixture.detectChanges fixture.detectChanges()调用ngOnInit并且我认为createComponent创建 class 并填充实例变量。 For a quick fix, try this:要快速修复,请尝试以下操作:

dataExtractService = TestBed.inject(DataExtractService); // get a handle on dataExtractService
// spy on their methods
spyOn(dataExtractService, 'getInstitutions').and.returnValue(of(expectedInstitution));
spyOn(dataExtractService, 'getDataSetTypes').and.returnValue(of(expectedDataSetType));
// create the component
fixture = TestBed.createComponent(ExtractRetrievalComponent);
component = fixture.componentInstance;

formBuilder = TestBed.inject(FormBuilder);
fixture.detectChanges();
it('should get filters', (done) => {
        const expectedInstitution = ['expectedInstitutions'] as string[];
        const expectedDataSetType = ['expectedDataSetType'] as string[];
        const expectedFilter = {
            institutions: expectedInstitution,
            dataSetTypes: expectedDataSetType
        };
        spyOn(component.filters$, 'subscribe').and.callThrough();

        component.filters$.subscribe(result => {
            expect(result).toEqual(expectedFilter);
            done();
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组件中的订阅未从共享服务接收值 - Subscription in Component not receiving values from shared service Observable 如何测试服务上的一系列 function 调用是否从同一服务上的 Observable 产生预期的发射值系列 - How to test that a series of function calls on a service produce the expected series of emitted values from an Observable on that same service 如何测试可观察的服务 - how to test an observable in service 如何将 observable 中的值存储在 aa 变量或属性中 - How to store values from an observable in a a variable or property 如果内部Observable在Observable of Observable流中失败,如何继续接收值? - How to keep receiving values if an inner Observable fails in a stream of Observable of Observables? Angular Service 被销毁,但为什么订阅仍然从 observable 接收? - Angular Service destroyed, but why is subscription still receiving from observable? 角度单元测试 - 如何窥探可观察的属性 - angular unit test - How to spyon observable property 如何模拟注入服务中的可观察属性? - How to mock observable property in injected service? 如何使用可观察服务测试组件错误处理 - How to test component error handling with observable service 如何在具有分配的组件中测试服务可观察 - How to test service observable in a component with assignments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM