Unit testing ngModel、定制管道和modal的单元测试错误

Unit testing ngModel、定制管道和modal的单元测试错误,unit-testing,angular,karma-jasmine,Unit Testing,Angular,Karma Jasmine,尝试测试我的Angular应用程序,但当尝试创建组件时,我会出现多个错误 无法绑定到“ngModel”,因为它不是“input”的已知属性 找不到“sortOnLike”管道 “应用程序编辑消息模式”不是已知元素: 如果“应用程序编辑消息模式”是一个角度组件,则验证它是否是此模块的一部分 如果“应用程序编辑消息模式”是Web组件,则将“自定义元素\u架构”添加到此组件的“@NgModule.schemas”以抑制此消息 类似错误的答案对我帮助不大 仪表板规格ts 从'@angular/core

尝试测试我的Angular应用程序,但当尝试创建组件时,我会出现多个错误

  • 无法绑定到“ngModel”,因为它不是“input”的已知属性
  • 找不到“sortOnLike”管道
  • “应用程序编辑消息模式”不是已知元素:
  • 如果“应用程序编辑消息模式”是一个角度组件,则验证它是否是此模块的一部分
  • 如果“应用程序编辑消息模式”是Web组件,则将“自定义元素\u架构”添加到此组件的“@NgModule.schemas”以抑制此消息
  • 类似错误的答案对我帮助不大

    仪表板规格ts

    从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
    从“@angular/Router”导入{Router}”;
    从“../../providers/MockAF”导入{MockAF}”;
    从“../../providers/AF”导入{AF};
    从“./dashboard.component”导入{DashboardComponent};
    描述('DashboardComponent',()=>{
    let组件:仪表板组件;
    let夹具:组件夹具;
    让routerStub;
    beforeach(异步(()=>{
    routerStub={
    导航:jasmine.createSpy('navigate')
    };
    TestBed.configureTestingModule({
    声明:[仪表板组件],
    供应商:[
    {provide:AF,useClass:MockAF},
    {提供:路由器,使用值:routerStub},
    ],
    })
    .compileComponents();
    }));
    在每个之前(()=>{
    fixture=TestBed.createComponent(仪表板组件);
    组件=fixture.componentInstance;
    fixture.detectChanges();
    });
    它('应该创建',()=>{
    expect(component.toBeTruthy();
    });
    });
    
    HTMLfile中的代码片段:

    dashboard.component.html

    
    邮寄
    编辑
    //情态形式
    
    来自dashboard.component.ts的片段

    import{Component,OnInit,AfterViewChecked,ElementRef,ViewChild}from
    '角/核';
    从'@angular/Router'导入{ActivatedRoute,Router};
    从“../../providers/AF”导入{AF};
    从“angularfire2”导入{FirebaseListObservable,AngularFire};
    从“./Bubble”导入{Bubble};
    从“../edit message model/edit”导入{EditMessageModalComponent}-
    消息模态组件';
    显示(键:字符串,消息:字符串):无效{
    this.modalMessage=消息;
    this.modalMessageKey=key;
    this.modal.show();
    }
    hide():void{
    this.modalMessage=null;
    this.modalMessageKey=null;
    this.modal.hide();
    
    }

    应用程序模块.ts

    从'@angular/platform browser'导入{BrowserModule};
    从“@angular/core”导入{NgModule};
    从'@angular/forms'导入{FormsModule};
    从'@angular/http'导入{HttpModule};
    从'@angular/router'导入{RouterModule,Routes};
    从'angularfire2'导入{AngularFireModule};
    从“./app.component”导入{AppComponent};
    从导入{RegistrationPageComponent}./注册页/注册-
    第页.组成部分';
    从“./login page/login page.component”导入{LoginPageComponent};
    从“./dashboard/dashboard.component”导入{DashboardComponent};
    从“../providers/AF”导入{AF};
    从“./frontscreen/frontscreen.component”导入{FrontscreenComponent};
    从“./student dashboard/student”导入{StudentDashboardComponent}-
    仪表盘.组件';
    从“./讲师仪表板/讲师”导入{讲师DashboardComponent}-
    仪表盘.组件';
    从“/config”导入{firebaseConfig};
    从导入{EditCourseModalComponent}./编辑课程模式/编辑课程-
    模态分量';
    从“.”导入{EditMessageModalComponent}/edit message model/edit-
    消息模态组件';
    从“./sort on like.pipe”导入{sortonlikepie}
    @NGD模块({
    声明:[
    应用组件,
    注册页面组件,
    LoginPageComponent,
    仪表板组件,
    前屏幕组件,
    StudentDashboard组件,
    讲师,董事会成员,
    EditCourseModalComponent,
    EditMessageModalComponent,
    ],
    进口:[
    浏览器模块,
    FormsModule,
    HttpModule,
    AngularFireModule.initializeApp(firebaseConfig),
    路由模块forRoot(路由),
    类球节
    ],
    提供者:[AF],
    引导:[AppComponent],
    })
    导出类AppModule{}
    
    在测试中,在每个块之前的
    内。您需要将以下内容添加到
    TestBed.configureTestingModule

  • 必须声明所有使用过的管道、组件和指令。在您的情况下:
    sortonlikepie
    EditMessageModalComponent
  • 所有使用过的模块都必须导入。在您的情况下:
    FormsModule
  • 必须提供所有必要的服务
  • 以下是您缺少的内容: 我想你可能会错过更多

    TestBed.configureTestingModule({
          declarations: [ DashboardComponent, SortOnLikePipe, EditMessageModalComponent  ],
          imports:[FormsModule]
          providers: [
            { provide: AF, useClass: MockAF },
            { provide: Router, useValue: routerStub },
          ],
        })
    

    1.在测试台中不包括
    表单模块
    。2.或者,就这一点而言,
    sortOnLike
    管道。3.或者
    app edit message model
    ,仔细想想。另请参见
    测试床中未包含组件依赖项的其他问题。你能详细谈谈“类似错误的答案对我没有多大帮助”,因为我已经找到了很多这样的答案——哪些答案,你尝试了什么,发生了什么?