简体   繁体   English

Jasmine 试图读取不存在的属性

[英]Jasmine trying to read property that doesn't exist

I have an extremely simple component:我有一个非常简单的组件:

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

@Component({
    selector: 'loading',
    templateUrl: './loading.component.html',
    styleUrls: ['./loading.component.scss']
})
export class LoadingComponent {
}

which just displays a static loading logo.它只显示一个 static 加载徽标。 No interactivity at all.完全没有交互性。 I wrote a test just to test its instantiation:我写了一个测试只是为了测试它的实例化:

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

import { LoadingComponent } from './loading.component';

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

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

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

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

In Firefox, this test throws an error: j is undefined , and in Chromium browsers I see Cannot read property 'startTime' of undefined .在 Firefox 中,此测试引发错误: j is undefined ,在 Chromium 浏览器中,我看到Cannot read property 'startTime' of undefined The template is simply:模板很简单:

Sample Text

for the sake of figuring out what's going on.为了弄清楚发生了什么。 So there's no mention of 'startTime' anywhere in those three files - what's going on??所以这三个文件中的任何地方都没有提到“startTime”——这是怎么回事?

Note笔记

I do use startTime properties in other components, but I can't figure out how/why that would matter.我确实在其他组件中使用了startTime属性,但我无法弄清楚这如何/为什么重要。

Edit编辑

I've been able to get this error to disappear by just adding:只需添加以下内容,我就可以使此错误消失:

afterAll(() => {
    TestBed.resetTestingModule();
});

to every single test in the project.到项目中的每一个测试。 I'm leaving the question open because I have no idea:我不知道这个问题,因为我不知道:

  1. Why this works/what the underlying problem was为什么这样有效/根本问题是什么
  2. If I'm swinging a heavier axe than I need to - would adding it to only one test suffice?如果我挥动的斧头比我需要的重 - 只将它添加到一个测试中就足够了吗?

The error you are seeing is definitely not coming from component and spec file you have posted.您看到的错误绝对不是来自您发布的组件和规范文件。 The error you are seeing is most likely coming from a component that does an async operation and the error occurs after the spec that instantiated it has already finished.您看到的错误很可能来自执行异步操作的组件,并且在实例化它的规范已经完成之后发生错误。

First off put an f in frontof the describes of this spec file.首先在这个规范文件的描述前面放一个 f。

fdescribe('LoadingComponent', () => {

This will cause the spec runner to only run this file.这将导致规范运行器仅运行此文件。 You will see that it passes fine.你会看到它通过了。

Next you want to remove the f and the search your project for startTime, try running specs for components that use the property startTime one at a time.接下来,您要删除 f 并在您的项目中搜索 startTime,尝试一次运行一个使用属性 startTime 的组件的规范。

If you can't locate it then put an x in front of the describes of them one at a time, eventually you will find which one is causing the error as it wont happen when you have an x on the spec that causes the error.如果您找不到它,然后一次在它们的描述前面放一个 x,最终您会发现哪个导致错误,因为当您在导致错误的规范上有 x 时,它不会发生。

Any specs that do async function should use the async function so that it waits for all async activity to finish before moving on to the next spec.任何执行异步 function 的规范都应使用异步 function 以便它等待所有异步活动完成,然后再继续下一个规范。

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

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