Angular单元测试错误:可以解析ExtConfig的所有参数:(?,?)

发布于 2025-01-25 14:45:04 字数 1563 浏览 5 评论 0原文

我是角度单位测试的新手。运行ng Test命令时,该单元测试失败了:

Can't resolve all parameters for ExtConfig: (?, ?)

如何解决此错误?

downloadscomponent.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { ExtConfig} from 'donwalods-portl';
import { Language } from '../../../language';

@Component({
    selector: 'lib-downloads',
    templateUrl: './downloads.component.html',
    styleUrls: ['./downloads.component.scss']
})

export class DownloadsComponent implements OnInit {

    constructor(
        private readonly configuration: ExtConfig;
    ) {
        this.lang = configuration.instance.languageMap as any as Language;
    }
  // more code
}

donwloadcomponent.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ExtConfig} from 'donwalods-portl'
import { DownloadsComponent } from './downloads.component';

describe('DownloadsComponent', () => {

    let fixture: ComponentFixture<DownloadsComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                DownloadsComponent
            ],
            providers: [{ provide: ExtConfig}]
        }).compileComponents();
    }));

    it('should create the component', () => {
        fixture = TestBed.createComponent(DownloadsComponent);
        const app = fixture.debugElement.componentInstance;
        expect(app).toBeTruthy();
    });
});

I am new to Angular unit tests. The unit test fails due to an error while running the ng test command:

Can't resolve all parameters for ExtConfig: (?, ?)

How can I fix this error?

downloadsComponent.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { ExtConfig} from 'donwalods-portl';
import { Language } from '../../../language';

@Component({
    selector: 'lib-downloads',
    templateUrl: './downloads.component.html',
    styleUrls: ['./downloads.component.scss']
})

export class DownloadsComponent implements OnInit {

    constructor(
        private readonly configuration: ExtConfig;
    ) {
        this.lang = configuration.instance.languageMap as any as Language;
    }
  // more code
}

donwloadComponent.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ExtConfig} from 'donwalods-portl'
import { DownloadsComponent } from './downloads.component';

describe('DownloadsComponent', () => {

    let fixture: ComponentFixture<DownloadsComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                DownloadsComponent
            ],
            providers: [{ provide: ExtConfig}]
        }).compileComponents();
    }));

    it('should create the component', () => {
        fixture = TestBed.createComponent(DownloadsComponent);
        const app = fixture.debugElement.componentInstance;
        expect(app).toBeTruthy();
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2025-02-01 14:45:04

看起来您的ExtConfig提供商具有一些依赖项,这些依赖项在testbed模块中未声明。

您将必须包括那些缺失的依赖项或模拟ExtConfig服务。

模拟提供商的一个示例可能是以下内容(请记住在方法中返回有效的内容):

export class ExtConfigMock {
    private readonly _context;
    private readonly _instance;
    constructor() {
        // Mock constructor
    }

    instance() {
        // Mocked method, return something
    };
    context() {
        // Mocked method, return something
    };
}

然后,在配置testbed模块时,请使用此模拟代替原始提供商:

beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                DownloadsComponent
            ],
            providers: [
              { provide: ExtConfig, useClass: ExtConfigMock }
            ]
        }).compileComponents();
    }));

It looks like your ExtConfig provider has some dependencies that are not declared in the TestBed module.

You'll have to include those missing dependencies or mock the ExtConfig service.

An example of the mocked provider could be the following (remember returning something valid in the methods):

export class ExtConfigMock {
    private readonly _context;
    private readonly _instance;
    constructor() {
        // Mock constructor
    }

    instance() {
        // Mocked method, return something
    };
    context() {
        // Mocked method, return something
    };
}

and then, when configuring the TestBed module, use this mock instead of the original provider:

beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                DownloadsComponent
            ],
            providers: [
              { provide: ExtConfig, useClass: ExtConfigMock }
            ]
        }).compileComponents();
    }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文