angular - ( Angular 单元测试)如何在 Jasmin 中模拟输入属性?

标签 angular typescript unit-testing karma-jasmine angular-unit-test

我目前正在尝试模拟 Angular 单元测试的输入属性。不幸的是,我无法再进一步并反复收到以下错误消息:

TypeError: Cannot read property 'data' of undefined

我的 HTML 模板如下所示

<div class="container-fluid">
  <div class="row">
    <div class="col-12">
      <plot [data]="graph.data" [layout]="graph.layout"></plot>
    </div>
  </div>
</div>

我的组件是这样的:

...
export class ChartComponent implements OnInit {

  @Input() currentChart: Chart;

  currentLocationData: any;

  public graph = {
    data: [
      {
        type: 'bar',
        x: [1, 2, 3],
        y: [10, 20, 30],
      }
    ],
    layout: {
      title: 'A simple chart',
    },
    config: {
      scrollZoom: true
    }
  };

  ...
}

我的单元测试现在看起来很基础,但仍然抛出上述错误:

describe('ChartComponent', () => {

  let component: ChartComponent;
  let fixture: ComponentFixture<ChartComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ChartComponent],
      imports: [
        // My imports
      ]
    })
      .compileComponents();
  }));

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

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

我尝试了不同的方法来模拟数据属性和 currentChart @Input

实现这一点并修复单元测试的正确方法是什么?

最佳答案

输入属性就像任何变量一样工作。在您的 beforeEach 中,您可以将其设置为一个值

beforeEach(() => {
  fixture = TestBed.createComponent(ExplorerChartViewComponent);
  component = fixture.componentInstance;
  component.currentChart = someChart; // set input before first detectChanges
  fixture.detectChanges();
});

您可以阅读更多关于此 here 的信息.我更喜欢 this approach .

使用我的首选方法,您将拥有一个看起来像这样的 TestHost 组件

@Component({
  selector: 'app-testhost-chart',
  template: `<app-chart [currentChart]=chart></app-chart>`, // or whatever your Chart Component Selector is
})
export class TestHostComponent {
  chart = new Chart();
}

然后切换到创建新的测试主机。

 declarations: [ChartComponent, TestHostComponent ],
...
beforeEach(() => {
  fixture = TestBed.createComponent(TestHostComponent );
  component = fixture.debugElement.children[0].componentInstance;
  fixture.detectChanges();
});

但是,我想我还看到了您可能遇到的另外两个问题。特别是因为您正在分配图表

  1. 您正在输入 declarations: [ChartComponent],但创建 fixture = TestBed.createComponent(ExplorerChartViewComponent);我认为应该TestBed.createComponent(ChartComponent) ,除非这是复制/粘贴问题。
  2. 您的 html 中有 <plot [data]="graph.data" [layout]="graph.layout"></plot>这表示您未声明的绘图组件。您将需要为绘图声明一个组件。我建议做一些与 TestHostComponent 非常相似的事情,但它与你的真实 PlotComponent 具有所有相同的公共(public)属性,这样你就不会将 PlotComponent 的真正功能和依赖项带入 ChartComponent 的单元测试中。

关于angular - ( Angular 单元测试)如何在 Jasmin 中模拟输入属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57972500/

相关文章:

unit-testing - 一个单元如何测试基于过程或基于事件的代码部分

angular - Observable.forkJoin 不起作用

javascript - 根据给定值选择相关的颜色代码

unit-testing - 在 Visual Studio 中使用 Catch2 进行单元测试的最佳实践

angular - 仅在当前 PageSize 上选择 Mat-Table?

javascript - 在 Response DTO 中公开自定义 getter 不起作用

python - 如果任何单元测试失败,如何使 Python 的覆盖工具失败?

Angular 2 : Dynamic component creation : AOT Compilation

angular - 如何使用 AngularFire、Firestore 和 Firebase 分页到上一页

Angular 2 - 自定义注释