angular5单元测试茉莉花+业力

时间:2020-07-06 06:07:30

标签: angular unit-testing karma-jasmine

我有一个要测试的简单方法:

ts文件:

policyOptions(event): void {
    this.selectedPolicy = event.srcElement["value"];
    this.detectFormValueChanges();
  }

spec文件,我写的是

it("policyOptions function test case", () => {
    const mockSelectedPolicy = {"value": "DEFAULT"};
    component.policyOptions(mockSelectedPolicy);
    component.detectFormValueChanges();
  });

html代码:

    <input id="enable" (change)="policyOptions($event)" type="radio" value="ENABLED" name="antiPhishingSelectPolicy" class="radio__input" [checked]="selectedPolicy==='ENABLED'">

我遇到错误:"value is undefined"。我究竟做错了什么?我该如何解决?

我刚开始接触角度测试,因此发现单元测试很难克服。请帮忙。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,虚拟数据格式不正确。这是测试用例:

it("policyOptions method test case, when DEFAULT is the value", () => {
    const testevent = {
      srcElement: {
        value: "DEFAULT",
      },
    };
    component.policyOptions(testevent);
    expect(component.selectedPolicy).toEqual("DEFAULT");
    component.policyOptions(testevent);
  });