如何在spec文件中创建构造函数的2D数组(带有2个参数) [英] How to create a 2D array of a contructor (with 2 arguments) in spec file

查看:72
本文介绍了如何在spec文件中创建构造函数的2D数组(带有2个参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与Jasmine和Karma在Angular中进行单元测试完全有关.

This question is entirely related to unit testing in Angular with Jasmine and Karma.

我有一个叫做 Strip 的组件.我有它的模板,它的打字稿,它的配置和它的测试用例,即spec文件.

I have one component called Strip. I have its template, its typescript, its configuration and its test case i.e. spec file.

strip.config.ts

// this is the configuration file for the StripComponent

import { WidgetSource } from '../../shared/models/widgetSource';

export class StripConfig {
    type: 'strip';
    rows: WidgetSource[][]; // <-------------- MY REQUIREMENT

    constructor() {}
}

这是上面的配置类已导入的 WidgetSource :

This is the WidgetSource that the above config class has imported:

widgetSource.ts

/**
 * Widget Source class.
 */

import { DashboardName, WidgetName } from './myJSONConfig';

export class WidgetSource {
    dashboardName: DashboardName;
    widgetName: WidgetName;

    constructor(
        dashboardName: DashboardName,
        widgetName: WidgetName
    ) {
        this.dashboardName = dashboardName;
        this.widgetName = widgetName;
    }
}

这是规格文件:

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

import { StripComponent } from './strip.component';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { StripConfig } from './strip.config';
import { WidgetSource } from 'src/app/shared/models/widgetSource';
import { DashboardName, WidgetName } from 'src/app/shared/models/myJSONConfig';

fdescribe('StripComponent', () => {
    let component: StripComponent;
    let fixture: ComponentFixture<StripComponent>;
    ...
    const strip: StripConfig = {
        type: 'strip',
        rows: new WidgetSource([DashboardName.MY_DASHBOARD], [WidgetName.HISTOGRAM]) // HELP
    };

    ...
});

如果它是config中这样的单个值:

If it was a single value like this in config:

rows: WidgetSource;

我在规范文件中的代码为:

My code in spec file would be:

rows: new WidgetSource(DashboardName.RPM_DASHBOARD, WidgetName.ACTIVE_DAYS_GRAPH)

但是我的要求是二维数组.请帮助我.

But my requirement is a double dimensional array. Please help me.

推荐答案

您可以像这样初始化它:

You could initialize it like this:

const strip: StripConfig = {
  type: 'strip',
  rows:
    // open here the array
    [
      // array consists of arrays so open here first second level array
      [
        // initialize here the WidgetSources
        new WidgetSource([DashboardName.MY_DASHBOARD], [WidgetName.HISTOGRAM]),
        new WidgetSource([DashboardName.MY_DASHBOARD], [WidgetName.HISTOGRAM])
      ],
      // add more second level arrays if you need
      [
        new WidgetSource([DashboardName.MY_DASHBOARD], [WidgetName.HISTOGRAM]),
        new WidgetSource([DashboardName.MY_DASHBOARD], [WidgetName.HISTOGRAM])
      ]
    ]
};

这里是TypeScript操场上的工作示例.

Here is working sample in TypeScript playground.

这篇关于如何在spec文件中创建构造函数的2D数组(带有2个参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆