角度测试:“非法状态:无法加载指令摘要"对于一个空的组件 [英] Angular testing: "Illegal state: Could not load the summary for directive" for an empty component
问题描述
我的所有组件都没有通过单元测试,因此我在项目中新建了一个组件以进行尝试和诊断.
None of my components were passing unit tests, so I made a new one in the project to try and diagnose.
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-thing',
templateUrl: './thing.component.html',
styleUrls: ['./thing.component.scss']
})
export class ThingComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
和一个thing.component.spec.ts
文件如下
describe('ThingComponent', () => {
let component: ThingComponent;
let fixture: ComponentFixture<ThingComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ThingComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ThingComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
测试失败并显示
Error: Illegal state: Could not load the summary for directive ThingComponent.
我已经用xit
关闭了所有其他测试,因此这是唯一正在运行的测试.
The test fails with
Error: Illegal state: Could not load the summary for directive ThingComponent.
I have turned off all other tests with xit
, so this is the only one running.
我试图将所有内容(提供程序,导入等)从我的app.module.ts
移到文件中,该文件仍然提供相同的结果.
I have tried to move in everything (providers, imports, etc) from my app.module.ts
into the file, which still provides the same result.
当我删除TestBed配置时,只有一个
When I remove the TestBed configuration, and just have a
const comp = new ThingComponent();
expect(comp).toBeTruthy();
通过.但是,这不是实际组件的长期解决方案.
this passes. However, that's not a long term solution for actual components.
事物的版本:
"@angular/cli": "^1.7.2",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-jasmine": "^1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
...
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
非常感谢您提供任何帮助
Any help at all would be appreciated, thanks
更新
我什至将测试更改为expect(true).toBeTruthy()
,它仍然出现相同的错误.在ng g component
提供的代码中,我没有进行任何其他更改.
Update
I even changed the test to expect(true).toBeTruthy()
and it still gets the same error. I've changed nothing else in the code that was provided by ng g component
.
此外,我在单独的目录中创建了一个新的角度项目,并且测试通过了这个新的空白项目.
Additionally, I made a new angular project in a separate directory, and the tests pass on this new blank project.
推荐答案
修复了该问题.我有一个具有spec.ts
文件的管道,如下所示:
Fixed it. I had a pipe which had a spec.ts
file as follows:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [SafeHtmlPipe],
providers: [DomSanitizer]
})
.compileComponents();
}));
describe('SafeHtmlPipe', () => {
let pipe;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [DomSanitizer]
});
});
beforeEach(inject([DomSanitizer], p => {
pipe = p;
}));
xit('create an instance', () => {
expect(pipe).toBeTruthy();
});
});
请注意如何使用xit
关闭测试.尽管如此,尽管被测组件未使用此管道,并且无论如何我还是在组件中导入/声明了它,但这还是导致了问题.
Note how the test is turned off with xit
. Despite this, and despite that the component under test was not using this pipe, and despite that I imported/declared it in the component anyway, this was causing the issue.
我不记得写这个了,所以也许它是自动生成的,因为正在测试的管道在构造函数中有一个DomSanitizer
.
I don't remember writing this, so maybe it was automatically generated, as the pipe it is testing has a DomSanitizer
in the constructor.
删除文件就可以了.
这篇关于角度测试:“非法状态:无法加载指令摘要"对于一个空的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!