【问题标题】:(Angular Unit-Test) How to mock input property in Jasmin?(角度单元测试)如何在 Jasmin 中模拟输入属性?
【发布时间】:2020-01-18 05:09:18
【问题描述】:

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

TypeError: 无法读取未定义的属性“数据”

我的 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

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

【问题讨论】:

  • expect 块之前的 spec 文件中执行 console.log(component.graph) 会得到什么?

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


【解决方案1】:

输入属性就像任何变量一样工作。在您的 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 包含 &lt;plot [data]="graph.data" [layout]="graph.layout"&gt;&lt;/plot&gt;,它表示您未声明的绘图组件。您将需要为绘图声明一个组件。我建议做一些与 TestHostComponent 非常相似的事情,但它与你的真实 PlotComponent 具有所有相同的公共属性,这样你就不会将 PlotComponent 的真正功能和依赖项带入 ChartComponent 的单元测试中。

【讨论】:

  • 感谢您的详细解释。现在测试运行良好,就像你说的:错误的 ExplorerChartComponent 声明是一个错字。
  • 非常感谢!!我找到的关于 Angular 输入测试的最佳解释!
猜你喜欢
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 2018-11-11
  • 2022-01-10
  • 2020-10-14
  • 2017-04-25
  • 1970-01-01
相关资源
最近更新 更多