unit-testing - 如何对 material2 图标进行单元测试

标签 unit-testing angular typescript angular2-directives angular-material2

此组件使用 Material 图标。

enter image description here

现在我正在尝试使用 karma 学习单元测试(通过 angular cli/webpack),并且我已经想出了创建组件的大部分配置,但我正在努力了解如何配置 Material 图标。

这是我到目前为止创建的内容:

enter image description here

/* config */
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
import { TickerDirective } from '../../directives/ticker.directive';
import { MdIconModule, MaterialModule } from '@angular/material';
import { MdIconRegistry } from '@angular/material/icon';

/* my stuff */
import { FoodListComponent } from './food-list.component';
import { FoodDataService } from '../../services/food-items/food-data.service';
import { FoodItem } from '../../diet/food-item';
import { WorkingData } from '../../services/working-data/working-data';
import { WorkingDataService } from '../../services/working-data/working-data.service';

describe('FoodListComponent', () => {
  let component:          FoodListComponent;
  let fixture:            ComponentFixture<FoodListComponent>;
  let foodDataService:    FoodItem[];
  let workingDataService: WorkingData;
  let de:                 DebugElement[];
  let el:                 HTMLElement;

  /* Stub Services */
  let foodDataServiceStub = [{
    name: 'test food name ..................', // written long to trigger the ticker directive
    img: './no_image.png',
    description: 'test food description'
  }];

  let workingDataServiceStub = {
    today: new Date(),
    selectedDate: new Date(2016, 2, 5),
    targetDate: new Date(2016, 2, 7),
    data: {exercise: 'Squat'}
  };

  beforeEach(async(() => {

    TestBed.configureTestingModule({
      declarations: [ FoodListComponent, TickerDirective ],
      imports: [ MaterialModule.forRoot(), MdIconModule], // not sure if this is correct
      providers: [
        { provide: FoodDataService, useValue: foodDataServiceStub },
        { provide: WorkingDataService, useValue: workingDataServiceStub } ,
        MdIconRegistry // not sure if this is correct
      ],
      schemas: [ NO_ERRORS_SCHEMA ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(FoodListComponent);
    component = fixture.componentInstance;

    /* Inject services */
    foodDataService = TestBed.get(FoodDataService);
    workingDataService = TestBed.get(WorkingDataService);

    /* Assign Services */
    component.workingData = workingDataService;
    component.foods = foodDataService;

    fixture.detectChanges();
    de = fixture.debugElement.queryAll(By.css('span'));
    el = de[0].nativeElement;
    // console.log(el);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
  it('should have the correct food name', () => {
    expect(el.textContent).toContain('test food name ..................');
  });
});

Material 图标 您可以看到 Material 图标的连字,但它们没有渲染。我读到我需要导入 Http 但这是通过错误导入的。

最佳答案

我在对我的组件进行单元测试时偶然发现了同样的问题,在互联网上一无所获后,我决定自己实现。

对于 Angular-CLI 用户: 在 test.ts 文件末尾添加以下代码段。

const materialIcons = document.createElement('link');
materialIcons.href = 'https://fonts.googleapis.com/icon?family=Material+Icons';
materialIcons.rel = 'stylesheet';
document.head.appendChild(materialIcons);

关于unit-testing - 如何对 material2 图标进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41026975/

相关文章:

java - 迁移到 java 11 时注释不起作用

angular - Angular 2 代理的 NPM Intellij IDEA 配置

javascript - 如何从 iframe 调用函数

返回 promise 时,Angular APP_INITIALIZER 不会延迟应用程序初始化

reactjs - 为什么 destroyOnClose={true} 在 React 中不起作用

c# - Ok Result 的单元测试测试

asp.net-mvc - 尝试使用 Moq 模拟 HtmlHelper 时抛出 MissingMethodException

c# - 如何让 VS 忽略 Test dll 的代码覆盖率

带有 ngrx 的 Angular 2 router v3 observable guard

javascript - 什么是用于 create-react-app 的 webpack.DefinePlugin 的良好替代品?