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