【问题标题】:How do i Unit test an angular component created with ngComponentOutlet?我如何对使用 ngComponentOutlet 创建的角度组件进行单元测试?
【发布时间】:2019-09-08 08:04:51
【问题描述】:

我有一个用 ngComponentOutlet 创建的组件

<ng-container *ngComponentOutlet="adminTableComponent; injector: adminTableInjector;"></ng-container>   

以及组件本身

import { Component, OnInit, Injectable, EventEmitter } from '@angular/core';
import { Data } from '@angular/router';

@Injectable()
export class AdminTableInfo {
  resourceData;
  resourceConfiguration;
  resourceName;
  constructor(resourceData, resourceConfiguration, resourceName) {
    this.resourceData = resourceData;
    this.resourceConfiguration = resourceConfiguration;
    this.resourceName = resourceName;
  }
}

@Component({
  selector: 'rw-admin-table',
  templateUrl: './admin-table.component.html',
  styleUrls: ['./admin-table.component.scss']
})
export class AdminTableComponent implements OnInit {

  private _resourceData;

  constructor(public adminTableInfo: AdminTableInfo) {
    if (adminTableInfo) {
      this._resourceData = adminTableInfo.resourceData;
    }
  }

  ngOnInit() {
    this.setTableStyle();
    this.setDefaultSort();
    this.adminTableInfo.resourceDataChanged$.subscribe(data => {
      this._resourceData = data;
      this.setDefaultSort();
    });
  }

...

如何设置单元测试?我们正在使用 Jest,但我认为设置应该与 Jasmine/Karma 非常相似

这是我目前的单元测试

@Injectable()
export class AdminTableInfo {
  resourceData;
  resourceConfiguration;
  resourceName;
  constructor(resourceData, resourceConfiguration, resourceName) {
    this.resourceData = resourceData;
    this.resourceConfiguration = resourceConfiguration;
    this.resourceName = resourceName;
  }
}

describe('AdminTableComponent', () => {
  let component: AdminTableComponent;
  let fixture: ComponentFixture<AdminTableComponent>;
  let fakeAdminTableInfo: AdminTableInfo;

  beforeEach(async(() => {
    MockConfiguration
    .getAdminTestBedConfiguration()
    .configureTestingModule({
      declarations: [
        AdminTableComponent,
        AdminTableRowComponent,
       ],
      providers: [
        AdminTableInfo
       ]
    })
    .compileComponents();
  }));

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

    // Not sure i need to create an instance like this of the injectable
    fakeAdminTableInfo = fixture.debugElement.injector.get(AdminTableInfo);

    fixture.detectChanges();
  });

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

我遇到了错误:

 Can't resolve all parameters for AdminTableInfo:
(?, ?, ?).

这意味着我在创建组件时没有注入 AdminTableInfo。 有谁知道如何为这种情况设置单元测试?

【问题讨论】:

    标签: angular unit-testing dependency-injection jestjs ng-component-outlet


    【解决方案1】:

    对于 Jasmine/Karma,您需要在 .overrideComponent() 中提供您的服务:

    @Injectable()
    export class AdminTableInfoSpy {
      resourceData;
      resourceConfiguration;
      resourceName;
      constructor(resourceData, resourceConfiguration, resourceName) {
        this.resourceData = resourceData;
        this.resourceConfiguration = resourceConfiguration;
        this.resourceName = resourceName;
      }
    }
    
    describe('AdminTableComponent', () => {
      let component: AdminTableComponent;
      let fixture: ComponentFixture<AdminTableComponent>;
      let fakeAdminTableInfo: AdminTableInfo;
    
      beforeEach(async(() => {
        MockConfiguration
        .getAdminTestBedConfiguration()
        .configureTestingModule({
          declarations: [
            AdminTableComponent,
            AdminTableRowComponent,
           ]
        })
        .overrideComponent(AdminTableComponent, {
          set: {
            providers: [
              { provide: AdminTableInfo, useClass: AdminTableInfoSpy }
            ]
          }
        })
        .compileComponents();
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(AdminTableComponent);
        component = fixture.componentInstance;
    
        // Not sure i need to create an instance like this of the injectable
        fakeAdminTableInfo = fixture.debugElement.injector.get(AdminTableInfo);
    
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    

    【讨论】:

    • 欢迎来到 SO!当你回答一个问题时,即使是对的,也试着解释一下你的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2020-07-10
    • 2018-06-18
    相关资源
    最近更新 更多