angular - Angular 7 测试失败,无法理解原因。有人帮我吗?
问题描述
我正在尝试检查联系人列表,不存在的事件。但是我的测试失败了。(见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
解决方案
确保您的组件中有联系人数组作为变量
推荐阅读
- javascript - 如何在不破坏所有 jquery 事件的情况下使用 Ajax 重新加载?
- android - Android:前台摄像头服务在大约 1 小时后停止拍摄照片
- c# - 我需要在 LINQ C# 中找到 3 个参数的平均值
- python - 查找列表中的最高和最低数字
- typescript - 如何将我的 serverless.ts 文件转换为 serverless.yaml 文件并使无服务器项目工作?
- python - MemoryError: Unable to allocate 301. MiB for an array with shape (13, 3032134) and data type object
- postgresql - 通过 Golang 更新我的 postgresql 数据库时遇到问题
- html - TH 元素不适合新的图像尺寸
- routes - 获取 RoutingSolver 或工具的中间解决方案
- spring-boot - 为 websocket 运行带有 https 后端的 Vue devServer 代理