如何为 Angular 6 Bootstrap 4 modal 进行茉莉花单元测试用例

2024-02-21

html

<ng-template #content let-modal>   
<h1>Modal content inside this ng-template #content </h1>  
</ng-template>

打开模型的按钮

<button  (click)="open(content)" > Open modal </button>

在ts文件中

import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
constructor(  public modalService: NgbModal) { }

open(content) {  
          this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title', size: 'lg' }).result.then((result) => {
            this.closeResult = `Closed with: ${result}`;
          }, (reason) => {
            this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
          });
           }

如何为此做一个茉莉花测试用例open功能。


这就是我过去测试过的方法...

假设组件 TS 文件如下所示:

export class MyComponent {

  closeResult: string;

  constructor(private _modalService: NgbModal) {}

  public openModal(content): void {
    this._modalService.open(content, { size: 'lg' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
       this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private getDismissReason(reason: any): string {
    return  `with: ${reason}`;
  }
}

您可以使用以下测试类来测试这些场景:

  1. this._modalService.open使用正确的参数调用
  2. 当模态关闭时,closeResult已正确更新
  3. 当模态被解除时,closeResult已正确更新

测试类如下所示:

import { TestBed, async, ComponentFixture, tick, fakeAsync } from '@angular/core/testing';
import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MyComponent } from './my.component';

// Mock class for NgbModalRef
export class MockNgbModalRef {
  result: Promise<any> = new Promise((resolve, reject) => resolve('x'));
}

describe('MyComponent', () => {

  let fixtureUnderTest: ComponentFixture<MyComponent>;
  let componentUnderTest: MyComponent;
  let modalService: NgbModal;
  let mockModalRef: MockNgbModalRef = new MockNgbModalRef();

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        MyComponent
      ],
      imports: [
        NgbModule.forRoot()
      ]
    }).compileComponents();

    fixtureUnderTest = TestBed.createComponent(MyComponent);
    componentUnderTest = fixtureUnderTest.componentInstance;
    modalService = TestBed.get(NgbModal);
  }));

  it('should open modal', () => {
    spyOn(modalService, 'open').and.returnValue(mockModalRef);
    componentUnderTest.openModal('<xxxx>');
    expect(modalService.open).toHaveBeenCalledWith('<xxxx>', { size: 'lg' });
  });

  // Needs to be async as modal result returned in a promise
  it('should update closeResult when modal closed', fakeAsync(() => {
    spyOn(modalService, 'open').and.returnValue(mockModalRef);

    componentUnderTest.openModal('<xxxx>');
    tick();
    expect(componentUnderTest.closeResult).toBe('Closed with: x');
  }));

  // Needs to be async as modal result returned in a promise
  it('should update closeResult when modal dismissed', fakeAsync(() => {
    spyOn(modalService, 'open').and.returnValue(mockModalRef);
    // Override the result returned from the modal so we can test what happens when the modal is dismissed
    mockModalRef.result = new Promise((resolve, reject) => reject('y'));

    componentUnderTest.openModal('<xxxx>');
    tick();
    expect(componentUnderTest.closeResult).toBe('Dismissed with: y');
  }));

});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为 Angular 6 Bootstrap 4 modal 进行茉莉花单元测试用例 的相关文章

随机推荐

  • Rails 6 无法连接到 AWS Elastic Beanstalk 预置的 RDS。 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”

    我在尝试向 Elastic Beanstalk 启动示例 Rails 6 应用程序时遇到了非常困难的情况 对于上下文 我遵循这些说明 将 RDS 添加到 Ruby 应用程序 https docs aws amazon com elastic
  • Python 中的线程队列挂起

    我正在尝试通过队列使解析器成为多线程 它似乎有效 但我的队列挂起 如果有人能告诉我如何解决这个问题 我将不胜感激 因为我很少编写多线程代码 此代码从 Q 中读取 from silk import import json import dat
  • 当父视图的任何子视图重绘时,如何触发父视图的重绘?

    背景 我写了一个基于 Android 的自定义视图LinearLayout我称之为ReflectingLayout 这个想法相当简单 在声明的任何子视图下面渲染反射效果ReflectingLayout e g 我通过覆盖来实现这一目标dis
  • C# NHibernate 简单问题

    我在用着NHibernate驱动存储库 Fluent映射并尝试使用Linq to NHibernate 但对于像这样的一些简单查询 Retrieve
  • 解析 HTML 内容时阻止 etree 解析 HTML 实体

    有没有办法阻止etree在解析HTML内容时解析HTML实体 html etree HTML amp html find body text 这给了我 但我想得到 本身 您始终可以对数据进行预处理 后处理 在输入 HTML 解析器之前将 替
  • 如何设置 10 点到 19 点每两小时执行一次 Cron 作业

    1 个月前我对此有一个疑问 那是1个小时的间隔 我得到了确切的答案 以下是旧问题的链接 如何设置从上午 9 00 到下午 6 00 周一至周五 每隔一小时执行一次 Cron 作业 https stackoverflow com questi
  • Python - Tkinter 文本大小未调整大小

    我正在尝试使用 Tkinter 制作一个可以调整大小的窗口 并且效果很好 但我希望字体大小也能按比例缩放 输入框的大小完美调整 但文本大小保持不变 我也可以更改输入文字大小吗 我该怎么做呢 先感谢您 这是到目前为止我的代码片段 defini
  • 压缩 XML 指标。

    我有一个客户端服务器应用程序 它通过 TCP IP 将 XML 从客户端发送到服务器 然后广播到其他客户端 我如何知道 XML 的最小大小可以通过压缩 XML 而不是通过常规流发送来保证性能改进 有什么好的指标或例子吗 Xml 通常压缩得很
  • 为什么有各种 JPEG 扩展名?

    在开发下载器时 我遇到了以下使用 Python 的情况mimetypes guess extension功能 In 2 mimetypes guess extension image jpeg strict False Out 2 jpe
  • 当 wifi 打开时,仅通过 Android 手机上的移动数据发送数据

    即使 wifi 已打开并连接到互联网 是否也可以通过移动数据以编程方式路由请求 我的应用程序需要调用运营商提供的服务 该服务只能通过移动数据使用 而且我认为要求关闭 wifi 不太方便 看一眼https developer android
  • 多对多关系的3表之间的SQL查询

    我有三张桌子 friends locations friend location friend location是一个连接表 允许多对多关系friends and locations 所以表格看起来像这样 Friends ID Name 1
  • WooCommerce:管理员手动创建订单时需要挂钩

    我的网站之一使用 WooCommerce 客户有时希望在订单管理中手动创建订单 WooCommerce gt 订单 gt 添加订单 当他们单击该页面上的 保存订单 时 我需要对订单进行一些额外的处理 有可用的钩子吗 我浏览了 WooComm
  • Microsoft 聊天机器人 (Node.js) 是否在单个 LUIS.AI 应用程序中支持多种语言?

    我有一个使用 Node js 在 Microsoft 机器人框架中构建的聊天机器人 并将该机器人与名为 LUIS AI 智能的 NLP 框架集成 以根据用户的意图和实体处理用户对话 在这里 我需要这个机器人在单个 LUIS 应用程序中支持多
  • awk 查找重叠

    我有一个包含列的文件 如下所示 Group Start End chr1 117132092 118875009 chr1 117027758 119458215 chr1 103756473 104864582 chr1 10509379
  • 寻找 C++ 中搜索和替换的圣杯

    最近 我正在寻找一种替换字符串中标记的方法 这本质上是查找和替换 但至少还有一种解决问题的方法 看起来像是相当平庸的任务 我已经提出了几种可能的实现 但从性能的角度来看 它们都不能令人满意 最好的成绩是每次迭代约 50us 这种情况很理想
  • 如何从构造函数内的原型对象检索属性

    我认为这个问题本身就很解释 我正在尝试检索构造函数对象内原型对象内部的特定属性 如果我无法检索它 我希望有人能解释为什么我不能检索它 这是来自的代码jsfiddle https jsfiddle net 786ze44b JavaScrip
  • 缩放级别 15 相当于什么?

    出于某种原因 这似乎比我想象的更难找到 我正在使用地图显示 并将缩放级别设置为 15 mapController setZoom 15 不同的缩放级别等于多少距离 我假设是某种对数或指数尺度 如果我选择 1 或 18 作为缩放级别 则地图上
  • DBIx::Class 中的子查询

    我在这上面花了太多时间 但仍然无法让语法起作用 这个 select 语句可以在 DBIx Class 中使用吗 SELECT A id A name count C a id AS count1 SELECT count B id FROM
  • Matplotlib 动画使用 ArtistAnimation 更新标题

    我正在尝试使用 ArtistAnimation 来创建动画 一切都正常 除了set title不工作 我不明白为什么blit False不起作用 我需要去吗 函数动画 https stackoverflow com questions 44
  • 如何为 Angular 6 Bootstrap 4 modal 进行茉莉花单元测试用例

    html