【发布时间】:2020-12-05 21:56:54
【问题描述】:
我有一个在模板中使用管道日期的组件。它适用于模拟器和浏览器。
执行单元测试时 - 我得到一个
错误:找不到管道“日期”!
对于该组件中的所有测试用例。使用最新的 ionic、angular、jasmine、karma 等。
app.module.ts
import { CommonModule, DatePipe } from '@angular/common';
@NgModule({
declarations: [AppComponent, DatePipe],
imports: [ Common Module ],
providers: [CommonModule],
exports: [CommonModule]
...
users.page.spec.ts
import { CommonModule, DatePipe } from '@angular/common';
describe('UsersPage', () => {
let component: UsersPage;
let fixture: ComponentFixture<UsersPage>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ CommonModule],
declarations:[DatePipe],
schemas: [ NO_ERRORS_SCHEMA ],
providers: [
{ provide: UserInfoService, useClass: MockUserInfoService },
{ provide: TaskHistoryService, useClass: MockTaskHistoryService },
{ provide: CommonModule, useClass: CommonModule}
],
}).compileComponents();
fixture = TestBed.createComponent(UsersPage);
component = fixture.componentInstance;
fixture.detectChanges();
});
错误:
Error: The pipe 'date' could not be found!
at getPipeDef$1 (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:24393:1)
at ɵɵpipe (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:24356:1)
at UsersPage_Template (ng:///UsersPage.js:115:9)
at executeTemplate (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:7329:1)
at renderView (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:7138:1)
at renderComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:8382:1)
at renderChildComponents (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:6997:1)
at renderView (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:7163:1)
at ComponentFactory$1.create (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:22291:1)
at initComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1983:1)
我也有 CommonModule 在使用日期管道的组件的@Component 的提供者中。感觉就像我错过了一些简单的东西。我已经阅读了类似的问题(针对自定义管道)并尝试应用相同的修复程序 - 到目前为止没有任何效果。提前致谢。
最后,一些版本号:
Angular CLI: 10.0.3
Node: 14.5.0
OS: darwin x64
Angular: 10.0.4
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: <error>
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.1000.3
@angular-devkit/build-angular 0.1000.3
@angular-devkit/build-optimizer 0.1000.3
@angular-devkit/build-webpack 0.1000.3
@angular-devkit/core 10.0.3
@angular-devkit/schematics 10.0.3
@angular/cli 10.0.3
@ngtools/webpack 10.0.3
@schematics/angular 9.1.7
@schematics/update 0.1000.3
rxjs 6.6.0
typescript 3.9.7
webpack 4.43.0
【问题讨论】:
标签: angular unit-testing ionic-framework