首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >角10:在执行测试时无法读取未定义的属性

角10:在执行测试时无法读取未定义的属性
EN

Stack Overflow用户
提问于 2020-07-06 06:36:11
回答 1查看 1.4K关注 0票数 0

为了学习角,我决定创建一个待办事项列表克隆。

我定义了以下接口和组件:

ListItem:

代码语言:javascript
运行
复制
import { Step } from './step';

export interface ListItem {
    id: number;
    description: string;
    done: boolean;
    steps: Step[];
}

步骤:

代码语言:javascript
运行
复制
export interface Step {
    description: string,
    done: boolean
}

step.component.ts

代码语言:javascript
运行
复制
import { Component, OnInit, Input } from '@angular/core';
import { ListItem } from '../listitem';
import { Step } from '../step';
import { StepService } from '../step.service';

@Component({
  selector: 'app-step',
  templateUrl: './step.component.html',
  styleUrls: ['./step.component.css']
})
export class StepComponent implements OnInit {

  @Input() listItem: ListItem;

  constructor(private stepService: StepService) { }

  ngOnInit(): void {
  }

  onSetStepDone(step: Step): void {
    this.stepService.setStepDone(step);
  }
}

step.component.html

代码语言:javascript
运行
复制
<div class="section-item">
    <span class="title">Steps</span>
    <ul class="steps">
        <li *ngFor="let step of listItem.steps">
            <div class="steps-body">
                <div *ngIf="step.done === true; else stepNotDone">
                    <div (click)="onSetStepDone(step)">
                        <span data-is-focusable="true" class="checkBox-sm completed big" role="checkbox" aria-checked="true" tabindex="0">
                            <i class="icon svgIcon ms-Icon checkbox-completed-20">
                                <svg focusable="false" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24">
                                    <path fill-rule="evenodd" d="M10.9854 15.0752l-3.546-3.58 1.066-1.056 2.486 2.509 4.509-4.509 1.06 1.061-5.575 5.575zm1.015-12.075c-4.963 0-9 4.037-9 9s4.037 9 9 9 9-4.037 9-9-4.037-9-9-9z"></path>
                                </svg>
                            </i>
                        </span>
                    </div>
                </div>

                <button tabindex="0" class="steps-titleWrapper">
                    <span class="listItem-title" [class.done]="listItem.done == true">
                        {{step.description}}  
                    </span>
                </button>
            </div>        
        </li>
    </ul>
            
</div>

<ng-template #stepNotDone>
    <div (click)="onSetStepDone(step)">
        <span data-is-focusable="true" class="checkBox-sm big" role="checkbox" aria-checked="false" tabindex="0">
            <i class="icon">
                <svg focusable="false" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24">
                    <path fill-rule="evenodd" d="M12 20c-4.411 0-8-3.589-8-8s3.589-8 8-8 8 3.589 8 8-3.589 8-8 8m0-17c-4.963 0-9 4.037-9 9s4.037 9 9 9 9-4.037 9-9-4.037-9-9-9"></path>
                </svg>
            </i>
            <i class="icon checkBox-sm-hover">
                <svg focusable="false" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24">
                    <g fill-rule="evenodd">
                        <path d="M12 20c-4.411 0-8-3.589-8-8s3.589-8 8-8 8 3.589 8 8-3.589 8-8 8m0-17c-4.963 0-9 4.037-9 9s4.037 9 9 9 9-4.037 9-9-4.037-9-9-9"></path>
                        <path d="M10.9902 13.3027l-2.487-2.51-.71.704 3.193 3.224 5.221-5.221-.707-.707z"></path>
                    </g>
                </svg>
            </i>
        </span>
    </div>
</ng-template>

当我尝试对step.component.spec.ts执行下面的测试时,我会得到一个错误,即不能读取属性步骤。

代码语言:javascript
运行
复制
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { StepComponent } from './step.component';

describe('StepComponent', () => {
  let component: StepComponent;
  let fixture: ComponentFixture<StepComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ StepComponent ]
    })
    .compileComponents();
  }));

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

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

我猜<li *ngFor="let step of listItem.steps">是测试失败的原因,但我找不出原因.我做错什么了吗?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-06 07:27:23

您需要在测试中初始化listItem。在实际的应用程序中,它的值是从父组件提供的,但是由于测试的范围是您的StepComponent,所以@输入没有提供任何值(所以它是未定义的)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62750493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档