简体   繁体   English

茉莉花角组件未定义

[英]Jasmine Angular component is undefined

I am a bit new to Angular 7 and totally new to it's unit testing framework Jasmine 我对Angular 7有点陌生,对它的单元测试框架Jasmine还是陌生的

So i have been following the docs of Jasmine and also some tutorials. 所以我一直在关注Jasmine的文档以及一些教程。 I have component called TestTableComponent . 我有一个名为TestTableComponent组件。 Now the component is a bit hard-coded. 现在,该组件已经过硬编码。 Anyways, i think the issue i'm facing hardly has anything to do with the component itself, so i am not including the component's code here. 无论如何,我认为我所面临的问题几乎与组件本身无关,因此在这里我不包括组件的代码。

I created a testing class, inside test-table.component.spec.ts . 我在test-table.component.spec.ts内部创建了一个测试类。 The code is as follows : 代码如下:

// Required Imports have been made. Not including as unnecessary.

describe('TestTableComponent',  async() => 
{
let component: TestTableComponent;
let fixture: ComponentFixture<TestTableComponent>;
let de: DebugElement;


beforeEach(async(() => 
{
    TestBed.configureTestingModule({declarations:[TestTableComponent],
    imports: [ReactiveFormsModule]}).compileComponents();
}));
beforeEach(() =>
{
    fixture = TestBed.createComponent(TestTableComponent);
    component = fixture.componentInstance;

    de = fixture.debugElement;
})


it('should check if data is returned by the API')
{

    const result = await component.GetEmployees;
    expect(result).toBeDefined();
}
});

The issue here is that, when i execute ng test , it seems to run the test based on this class. 这里的问题是,当我执行ng test ,它似乎基于此类运行测试。 In the console of the browser, i get one error(for which, Jasmine says 1 component is pending ) as follows : 在浏览器的控制台中,我得到一个错误(对此, Jasmine1 component is pending处于1 component is pending ),如下所示:

Cannot read property GetEmployees of undefined. 无法读取未定义的属性GetEmployees

Now, this clearly means that TestTableComponent is never initialized. 现在,这显然意味着TestTableComponent永远不会初始化。 I am just wondering why? 我只是想知道为什么? Is beforeEach not being executed? 难道不是beforeEach都被执行? If it is, then why is component undefined ? 如果是,那么为什么component未定义?

UPDATE : Including the component's code 更新:包括组件的代码

Test-table.component.ts 测试表.component.ts

import { AfterViewInit, Component, OnInit, ViewChild, Inject } from '@angular/core';
import { MatPaginator, MatSort, MatTable, MatTableDataSource, MatDialogRef, Sort, ShowOnDirtyErrorStateMatcher } from '@angular/material';
import { MatDialog } from '@angular/material/dialog';
import { TestTableItem } from './test-table-datasource';
import { HttpClient, HttpParams } from '@angular/common/http';
import { UpdateModalDialogComponent } from '../update-modal-dialog/update-modal-dialog.component';
import { MessagePopUpComponent } from '../message-pop-up/message-pop-up.component';
@Component({
selector: 'app-test-table',
templateUrl: './test-table.component.html',
styleUrls: ['./test-table.component.css']
})
export class TestTableComponent implements AfterViewInit, OnInit {
@ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
@ViewChild(MatSort, { static: false }) sort: MatSort;
@ViewChild(MatTable, { static: false }) table: MatTable<TestTableItem>;
private myCollection: TestTableItem[] = [];

dataSource = new MatTableDataSource(this.myCollection);// Observable<TestTableItem>;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['id', 'fname', 'salary', 'age', 'image', 'actions'];
constructor(private http: HttpClient, private dialog:MatDialog) { }


ngOnInit() {

   this.GetAllEmployees();
 }

async GetAllEmployees()
{
 this.dataSource = await this.GetEmployees();
}
public async GetEmployees()
{
 this.myCollection = await this.http.get<TestTableItem[]>('http://localhost:22371/api/employee/GetEmployees/').toPromise();
return new MatTableDataSource(this.myCollection);

}

Please note that i am not including all the functions inside the class, as that would make this post unnecessarily big! 请注意,我没有将所有函数包含在类中,因为这会使这篇文章变得不必要!

You writing the wrong it() function syntax, it should take first parameter is the string description and second parameter is the callback that implement your test: 您编写了错误的it()函数语法,它的第一个参数是字符串说明,第二个参数是实现测试的回调:

it('should check if data is returned by the API', async(() =>{
    fixture.detectChanges();
    const result = component.GetEmployees();
    fixture.whenStable().then(()=>{
      expect(result).toBeDefined();
    })
}))

Besides the wrong it() syntax @Ethan mentioned. 除了提到了错误的it()语法@Ethan之外。 You need to set either NO_ERRORS_SCHEMA to your TestBed or you need to include missing dependencies into your TestBed. 您需要将NO_ERRORS_SCHEMA设置到您的TestBed,或者需要在TestBed中包括缺少的依赖项。

I personally prefer the NO_ERRORS_SCHEMA approach since imho a unit test does not need to test whether some third party library works right, but that is up to you. 我个人更喜欢NO_ERRORS_SCHEMA方法,因为恕我直言,单元测试不需要测试某些第三方库是否工作正常,但这取决于您。 This approach is usually referred to as shallow testing a component 这种方法通常称为浅测试组件

The schema is set like this: 模式设置如下:

TestBed.configureTestingModule({
    declarations:[TestTableComponent],
    imports: [ReactiveFormsModule],
    schemas: [NO_ERRORS_SCHEMA]

}).compileComponents();

Please have a look at the official documentation on nested component tests 请查看有关嵌套组件测试的官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM