Angular 7 тестирование не проходит, не в состоянии понять причину.Кто-нибудь поможет мне? - PullRequest
0 голосов
/ 15 декабря 2018

Я пытаюсь проверить список контактов, событие без существования.Но мой тест провалился.(см. 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

1 Ответ

0 голосов
/ 15 декабря 2018

Убедитесь, что у вас есть массив контактов в качестве переменной в вашем компоненте

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...