【发布时间】:2017-04-20 22:02:03
【问题描述】:
我正在开发一个新项目,并将使用 CLI 生成应用程序和所有文件,但是我遇到了一些对我来说似乎很臭的东西,我希望有更好的方法来做到这一点.
我的应用有路由,所以在我的测试中我需要导入RouterTestingModule 来为路由器提供模拟。但是,由于我们创建的每个规范都将需要它,因此如果在创建新组件时默认包含它,那就太好了。我研究了对自定义蓝图的支持,但目前还没有任何支持,这很糟糕,因为如果我可以将该模块添加到蓝图中,这将非常简单。
还有哪些其他选项可以默认将其包含在所有规范中,而无需每个开发人员在创建新组件时记住添加它?
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
imports: [
RouterTestingModule, // I don't want to have to manually add this in every spec file.
SharedModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
【问题讨论】:
-
在模块上为所有可重用的导入创建并将其包含在此处。
-
我做到了,我在
SharedModule中包含了RouterModule的实际导入,你会看到我在规范中导入。问题是在测试中我需要导入RouterTestingModule而不是RouterModule才能运行测试。 -
好吧,将
RouterTestingModule包含在应用程序中而不是仅将其包含在规范中似乎并没有什么坏处。
标签: angular karma-jasmine angular2-router