Angular单元测试错误:可以解析ExtConfig的所有参数:(?,?)
我是角度单位测试的新手。运行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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的
ExtConfig
提供商具有一些依赖项,这些依赖项在testbed
模块中未声明。您将必须包括那些缺失的依赖项或模拟
ExtConfig
服务。模拟提供商的一个示例可能是以下内容(请记住在方法中返回有效的内容):
然后,在配置
testbed
模块时,请使用此模拟代替原始提供商:It looks like your
ExtConfig
provider has some dependencies that are not declared in theTestBed
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):
and then, when configuring the
TestBed
module, use this mock instead of the original provider: