Angular 4 - 如何在Karma测试中解析带有连字符的变量在模板中?
创始人
2024-10-15 18:30:21
0

在Karma测试中解析带有连字符的变量在Angular模板中的方法如下:

  1. 将变量名中的连字符转换为驼峰命名法,例如将 my-variable 转换为 myVariable

  2. 在测试组件中声明一个与转换后的变量名相同的变量,并将其赋值为所需的值。例如:

component.myVariable = 'Value';
  1. 在测试中使用 fixture.detectChanges() 来触发变更检测。

  2. 使用 fixture.nativeElement 来获取渲染后的模板,并通过查询选择器选择包含变量的元素。例如:

const element = fixture.nativeElement.querySelector('.my-class');
  1. 断言元素的属性或文本是否正确。例如:
expect(element.textContent).toContain('Value');

完整的示例代码如下:

import { TestBed, ComponentFixture } from '@angular/core/testing';
import { MyComponent } from './my.component';

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

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [MyComponent]
    });

    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
  });

  it('should render variable with hyphen in template', () => {
    component.myVariable = 'Value';
    fixture.detectChanges();

    const element = fixture.nativeElement.querySelector('.my-class');
    expect(element.textContent).toContain('Value');
  });
});

在上面的示例中,我们假设有一个带有 myVariable 变量的组件,并且模板中有一个类名为 my-class 的元素,我们想要测试该元素是否正确渲染了变量的值。

相关内容

热门资讯

传递经验!wepoker辅助是... 传递经验!wepoker辅助是真的吗,wpk模拟器多开,力荐教程(有挂方法)1、玩家可以在wepok...
盘点一款!wepokerplu... 此外,数据分析德州(wepokerplus透视脚本免费)辅助神器app还具备辅助透视行为开挂功能,通...
最新通报!wejoker内置辅... 最新通报!wejoker内置辅助,红龙poker辅助,大神讲解(有挂软件)是由北京得wejoker内...
一分钟了解!wepoker私人... 一分钟了解!wepoker私人局外卦,wepoker安装教程,AI教程(有挂方法)这是由厦门游乐互动...
2分钟细说!佛手大菠萝有挂吗,... 2分钟细说!佛手大菠萝有挂吗,德普之星有辅助软件吗,安装教程(有挂透视)准备好在德普之星有辅助软件吗...
大家学习交流!wepoker有... 大家学习交流!wepoker有脚本吗,wepoker插件下载,2025新版教程(有挂辅助)准备好在w...
新手必备!werplan透视挂... 新手必备!werplan透视挂,wepoker透视是真的吗,wpk教程(有挂辅助);最新版2024是...
解密关于!hhpoker是真的... 解密关于!hhpoker是真的还是假的,wepoker的辅助器,实用技巧(有挂软件);hhpoker...
一分钟揭秘!wepoker私人... 一分钟揭秘!wepoker私人局外卦,wepoker安装教程,安装教程(有挂教程);支持2-10人实...
记者发布!wepoker线上大... 记者发布!wepoker线上大神,wpk俱乐部辅助器,第三方教程(有挂辅助);科技安装教程;1367...