当前位置: 首页 > 知识库问答 >
问题:

茉莉花测试:预期未定义等于Object。(如何测试@input)

凌和悦
2023-03-14

我有这个错误的麻烦。我想我忘记了一些东西,无法弄清楚是什么。任务很简单:只需测试我的简单组件。在这里,我们去:

import { Component, OnInit, Input } from '@angular/core';
import { ButtonModel } from './model/button.model';

@Component({
  selector: 'app-button',
  templateUrl: './button.component.html',
  styleUrls: ['./button.component.scss']
})
export class ButtonComponent implements OnInit {
  @Input() public button: ButtonModel;

  constructor() { }

  public ngOnInit(): void {
  }

  public getClasses(): [string, string] {
    return [
      this.button.Size,
      this.button.Color
    ];
  }
}

和测试:

import { Spectator, createHostFactory } from '@ngneat/spectator';
import { RouterTestingModule } from '@angular/router/testing';
import { ButtonComponent } from './button.component';
import { MockComponent } from 'ng-mocks';


describe('[ButtonComponent]', () => {
  let spectator: Spectator<ButtonComponent>;
  const createHost = createHostFactory({
    component: MockComponent(ButtonComponent),
    imports: [RouterTestingModule]
  });

  beforeEach(() => {
    spectator = createHost('<app-button></app-button>');
  });

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

  it('should get default classes', () => {
    expect(spectator.component.getClasses()).toEqual(['big', 'primary-theme-color']);
  });
});

和HTML:

<button [ngClass]="getClasses()">
  {{button.Text}}
</button>

这个错误:

需要undefined等于['big','primary theme color']。错误:需要undefined等于['big','primary theme color']。位于UserContext。(http://localhost:9876/_karma_webpack_/src/app/shared/components/button/button.component.spec.ts:24:46)ZoneDelegate。调用(http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-常绿植物。js:359:1)。onInvoke(调用)(http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)

共有2个答案

柴坚诚
2023-03-14

在没有主机组件和设置按钮属性值的情况下进行测试对我有效,

  let spectator: Spectator<ButtonComponent>;   
  const createComponent = createComponentFactory(ButtonComponent);

  beforeEach(() => {
    spectator = createComponent({
      props: {
        button: {Size: "big", Color: 'primary-theme-color'}
      }
    });
  });

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

  it('should get default classes', () => {
    expect(spectator.component.getClasses()).toEqual(['big', 'primary-theme-color']);
  });
漆雕硕
2023-03-14

您正在使用 Spectator 进行测试,您可以阅读该工具的文档:https://github.com/ngneat/spectator

在您的情况下,它将是< code > spectator . set input(' button ',{Text: 'my text ',Size:' ... ',颜色: '...'})在您的< code >期望之前

 类似资料:
  • 我熟悉python单元测试测试,如果断言失败,该测试将被标记为“失败”,并继续进行其他测试。另一方面,茉莉花将继续通过所有期望,即使其中一个失败。在第一个预期失败后,如何使Jasmine停止处理测试? 我想错了吗?我有一些测试有很多< code>expect,当只有第一个错误时,显示所有的堆栈跟踪似乎是一种浪费。

  • 我正试图写一个Jasmine测试来覆盖一个Twitter Boostrap模态对话框。当调试器行被注释掉时,测试失败。当调试器暂停处理并且我继续时,它通过。我认为引导模式的转换导致了这个问题,因为在我进行expect调用时,模式对话框还不在DOM中。 如何在测试期间禁用转换? 谢谢 感谢Jarred,您的解决方案非常有效!这是我的工作测试:

  • 问题内容: 我的控制器具有如下代码: 在我的单元测试中,我正在做这样的事情: 现在在测试中,我正在检查是否调用了服务,并且data1,data2是否未定义。 我的问题是,这是工作的罚款,直到我取代与q.all控制器和测试我个人的服务电话与。使用q.all和(也尝试使用),两个测试均失败,并显示以下错误: 达到10个$ digest()迭代。流产! 如果我删除了,那么承诺永远不会得到解决,而测试失败

  • 我刚刚安装了。我试图将添加到测试中,但遇到错误 ReferenceError:未定义beforeAll 查看堆栈跟踪时,我看到jasmine 1.3.1是量角器的子模块: C:\用户\bmackey\AppData\漫游\npm\node_modules\量角器\node_modules\mini jasminenode\lib\jasmine-1.3.1.js:913: 21 jasmine 2

  • 我需要用jasmine对一些DOM操作函数进行单元测试(目前我在浏览器和Karma中运行测试) 我想知道最好的方法是什么? 例如,我可以模拟和存根窗口,记录对象和spyOn的几个函数。但这看起来并不是一个简单的解决方案,所以我要问这个问题! 或者有没有更好的方法(也许不用茉莉花)来做到这一点? 非常感谢

  • 问题内容: 我正在尝试测试响应拦截器,但是很难弄清楚如何模拟$ window对象。这是我的拦截器代码: 这是我的规格: 我有一个:。关于如何正确模拟$ window对象或更一般地说如何测试401 +重定向情况的任何帮助? 问题答案: 您应该使用最新的语法来构造拦截器定义。您的URL构造也应包含在服务中,以便可以在测试中轻松模拟它。 这样做可以让您像其他任何工厂一样对其进行测试,而不必担心拦截器的内