首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取错误:由使用jest+spectator angular 9的模块'DynamicTestModule‘声明的意外值'undefined’

获取错误:由使用jest+spectator angular 9的模块'DynamicTestModule‘声明的意外值'undefined’
EN

Stack Overflow用户
提问于 2020-02-18 07:45:28
回答 1查看 1K关注 0票数 1

我正在使用angular 9,并在深入研究后替换了angular的默认单元测试给旁观者和jest,它很有趣,我有几个测试运行良好,但突然我有这个问题,当我试图测试一些父组件,产生丑陋的错误(见问题标题),我不知道如何解决,将很高兴得到一些帮助,代码如下

其他类似的测试也在运行,但这个测试在标题上得到了不友好的消息,即使我删除了模板的所有标记,只编写了愚蠢的div,并取消了测试上的模仿,但我仍然得到了这个令人沮丧的……已尝试在没有帮助的情况下重新启动服务器。

spec.ts

代码语言:javascript
复制
    import { Spectator, createComponentFactory } from '@ngneat/spectator/jest';
    import { PlannerScreenComponent } from './planner-screen.component';
    import { SlotsListComponent } from '../planner/components/slots-list/slots-list.component';
    import { MockComponent } from 'ng-mocks';

    describe('PlannerScreenComponent suit', () => {
      let spectator: Spectator<PlannerScreenComponent>;
      const createComponent: () => Spectator<PlannerScreenComponent> = createComponentFactory({
        component: PlannerScreenComponent,
        declarations: [MockComponent(SlotsListComponent)]
      });

      beforeEach(() => (spectator = createComponent()));

      it('should create plannerScreenComponent ', () => {
        const plannerScreenComponent = spectator.component;
        expect(plannerScreenComponent).toBeTruthy();
      });
    });

component.ts

代码语言:javascript
复制
    import { Component, OnInit, Input } from '@angular/core';

    @Component({
      selector: 'planner-screen',
      templateUrl: './planner-screen.component.html',
      styleUrls: ['./planner-screen.component.scss']
    })
    export class PlannerScreenComponent implements OnInit {
      @Input() isSplittedScreen = false;
      constructor() {}

      ngOnInit() {}
    }

component.html

代码语言:javascript
复制
    section [class.splitted-screen]="isSplittedScreen" class="planner-screen-wrapper">
          <aside class="left-ruller-container"></aside>
          <div class="top-middle-title">
            <div>title</div>
          </div>
          <div class="main-panel-container">
            <slots-list></slots-list>
          </div>
          <aside class="right-ruller-container"></aside>
        </section>
EN

回答 1

Stack Overflow用户

发布于 2020-02-23 03:50:02

我不能理解你测试中的这一行

代码语言:javascript
复制
declarations: [MockComponent(SlotsListComponent)]

您没有在PlannerScreenComponent中使用SlotListComponent,因此没有必要在测试中模拟此组件。我认为,您可以自由地删除这一声明行。在此之后,测试工作正常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60271894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档