加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

单元测试 – 使用构造函数参数测试Angular 2组件

发布时间:2020-12-17 17:00:30 所属栏目:安全 来源:网络整理
导读:假设我有一个带有两个输入参数的Angular 2 Component: @Component{... (omitted for clarity)}export class SomeComponent {@Input() a: number@Input() b: number} 当我想测试这个组件时,我有类似的东西: beforeEach(async(() = { TestBed.configureTesti
假设我有一个带有两个输入参数的Angular 2 Component:

@Component{... (omitted for clarity)}
export class SomeComponent {

@Input() a: number
@Input() b: number

}

当我想测试这个组件时,我有类似的东西:

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        SomeComponent,],})
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(SomeComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

createComponent调用不接受任何参数或允许我调用构造函数.如何为各种数值实例化/测试组件?

解决方法

正如JB Nizet指出的那样,当一个组件有@input参数时,你需要在beforeEach()中初始化它们:
“`

beforeEach(() => {
    fixture = TestBed.createComponent(SomeComponent);
    component = fixture.componentInstance;
    component.a = 1;
    component.b = 2;
    fixture.detectChanges();
});

“`

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读