Angular 8 - 单元测试加载 mat-modules 的问题
创始人
2024-10-17 10:30:31
0

在Angular 8中,如果你在单元测试中遇到了加载mat-modules的问题,可以按照以下步骤解决:

  1. 在你的组件.spec.ts文件中,导入MatButtonModule或者你需要的其他mat-modules。
import { MatButtonModule } from '@angular/material/button';
  1. 在describe函数中,使用beforeEach()函数导入mat-modules。
import { TestBed, async } from '@angular/core/testing';
import { MatButtonModule } from '@angular/material/button';

describe('YourComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        MatButtonModule
      ],
      declarations: [
        YourComponent
      ],
    }).compileComponents();
  }));
});
  1. 如果你的组件中使用了mat-modules,例如使用MatButtonModule中的按钮组件,那么你需要在测试用例中对该组件进行引用。
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { MatButtonModule } from '@angular/material/button';

import { YourComponent } from './your.component';

describe('YourComponent', () => {
  let component: YourComponent;
  let fixture: ComponentFixture;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        MatButtonModule
      ],
      declarations: [
        YourComponent
      ],
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(YourComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

通过以上步骤,你应该能够在Angular 8的单元测试中成功加载mat-modules。请注意,你可能需要根据你的具体情况调整以上代码示例。

相关内容

热门资讯

透视关于"we-po... 透视关于"we-poker正规吗"切实有辅助插件(哔哩哔哩)1、很好的工具软件,可以解锁游戏的we-...
透视有挂"wepok... 透视有挂"wepoker俱乐部辅助"其实存在有辅助插件(哔哩哔哩)1、wepoker俱乐部辅助免费辅...
透视详细"poker... 透视详细"pokerworld辅助器"竟然真的是有辅助工具(哔哩哔哩)一、pokerworld辅助器...
透视详细"wepok... 透视详细"wepoker永久免费脚本"竟然真的是有辅助教程(哔哩哔哩)1、wepoker永久免费脚本...
透视解谜"hhpok... 透视解谜"hhpoker作必弊码"真是存在有辅助工具(哔哩哔哩)1、许多玩家不知道hhpoker作必...
透视辅助"wepok... 透视辅助"wepoker私人局怎么玩"真是是有辅助app(哔哩哔哩)1、wepoker私人局怎么玩免...
透视总结"wpk免费... 透视总结"wpk免费辅助"一贯有辅助技巧(哔哩哔哩)1、让任何用户在无需wpk免费辅助安装教程第三方...
透视分享"wepok... 透视分享"wepoker辅助器是真的吗"一直存在有辅助软件(哔哩哔哩)1、打开软件启动之后找到中间准...
透视教你"wepok... 透视教你"wepoker作必弊方法"竟然真的是有辅助技巧(哔哩哔哩)wepoker作必弊方法辅助器是...
透视教你"wpk可以... 透视教你"wpk可以作必弊吗"确实真的是有辅助神器(哔哩哔哩)1、超多福利:超高返利,海量正版游戏,...