角度测试错误:无法解析服务的所有参数:

问题描述 投票:2回答:2

我是新手测试。我有一个service和一个spec文件,运行时我收到以下错误:

错误:无法解析DashboardService的所有参数:(?)。错误属性:Object({ngSyntaxError:true})

spec文件如下所示:

import { TestBed } from '@angular/core/testing';
import { DashboardService } from './dashboard.service';
import { ApiService } from './../api.service';

describe('The Dashboard Service', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({ providers: [DashboardService, ApiService] });
    });

    it('should be created', () => {
        const service: DashboardService = TestBed.get(DashboardService);
        expect(service).toBeTruthy();
    });
});

到目前为止,service看起来像这样:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiService } from './../api.service';
import { Organization } from '../../models/organization';

@Injectable({
    providedIn: ApiService
})
export class DashboardService {
    constructor(private api: ApiService) {}

    getPrograms(id: number): Observable<any> {
        let url  = '/apiurl' + id;
        return this.api.get<Organization>(url);
    }
}

所以我猜错误是因为service文件的依赖性,但在阅读Angular文档后,我仍然不确定如何让Angular知道这些依赖关系。如何构造spec文件以正确读取依赖项?

angular testing karma-jasmine angular-test angular-testing
2个回答
1
投票

这样的事情怎么样?

第一:

import { inject } from '@angular/core/testing';

然后:

it('should be created', inject([DashboardService], (dashboardService: DashboardService) => {
  expect(dashboardService).toBeTruthy();
}));

0
投票

我将import 'core-js/es7/reflect';添加到test.ts文件中。

© www.soinside.com 2019 - 2024. All rights reserved.