Angular 7测试失败,无法理解原因。有人帮帮我吗?

问题描述 投票:-1回答:1

我正在尝试检查联系人列表,没有存在的事件。但我的测试失败了。 (见component.contacts.length

这是我的测试代码:contacts.component.spec

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ContactsComponent } from './contacts.component';
import { ContactClass } from './../shared/models/contact'

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

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

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

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

  it('should be no contacts if there is no data', () => {
    expect(component.contacts.length).toBe(0); ---> fails!!
  });

});

我的组件:

import { Component, OnInit } from '@angular/core';

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

    constructor() { }

    ngOnInit() {
    }

}

错误:

ContactsComponent Tests should be no contacts if there is no data
TypeError: Cannot read property 'length' of undefined
angular angular7
1个回答
0
投票

确保在组件中将contacts数组作为变量

© www.soinside.com 2019 - 2024. All rights reserved.