角度测试错误:无法解析服务的所有参数: [英] Angular Testing Error: Can't resolve all parameters for Service:

查看:117
本文介绍了角度测试错误:无法解析服务的所有参数:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是测试新手.我有一个servicespec文件,运行时出现以下错误:

I'm new to testing. I have a service and a spec file that when run I get the following error:

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

Error: Can't resolve all parameters for DashboardService: (?). error properties: 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看起来像这样:

The service looks like this so far:

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文件以正确读取依赖关系?

So I guess the error is because of the dependencies to the service file but after reading the Angular documentation I'm still not sure of how to let Angular know about these dependencies. How do I structured the spec file to read dependencies correctly?

推荐答案

这样的事情怎么办?

第一:

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

然后:

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

这篇关于角度测试错误:无法解析服务的所有参数:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆