微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

测试无法识别 ngOnInit 组件:我的测试:我在运行测试时遇到的错误:

如何解决测试无法识别 ngOnInit 组件:我的测试:我在运行测试时遇到的错误:

我想为组件中的某些功能添加测试。 在我的组件的 ngOnInit 中,我正在调用一个ChWidget 服务交互的函数并且它工作正常。但测试失败,因为 ChWidgetundefined

我试图模拟这一步,但对我来说效果不佳。

您能否建议是否有适当的处理方法?谢谢!

组件:

import { Component,OnInit,Input } from '@angular/core';

import { ActionsService } from '@ch-common/ch-common.module';
import { Product } from '@ch-products/product';
declare var window;

@Component({
  selector: 'ch-container-header',templateUrl: './container-header.component.html',styleUrls: ['./container-header.component.scss']
})
export class ContainerHeaderComponent implements OnInit {
  @input() container: any;

  product_group: any;

  constructor(private actions: ActionsService) { }

  ngOnInit() {
    if (this.isProductList()) {
      this.product_group = this.getGroupDetails(this.container);
    }
  }

  getGroupDetails(container) {
    var product = new Product(container.nodes[0].params.product)
    var group_id = window.ChWidget.utils.getQueryStringParam("product-group-id",document.location.href)

    return product.productBadgeIcons.find(obj => obj["group_id"] == group_id);
  }

  gotActions() {
    return (this.container.params.actions || []).length > 0;
  }
}

我的测试:

function setComponentParams(params) {
    component.container = params;
  }

  function renderComponentWithParams(params) {
    fixture = Testbed.createComponent(ContainerHeaderComponent);
    component = fixture.componentInstance;

    setComponentParams(params);
    fixture.detectChanges();
    compiled = fixture.debugElement.nativeElement;
  }


  fit('renders the show all if there is an action on the container and opens a product group page when clicking it',() => {
    var container = {
      nodes: [
        {params: {
            product: {
              group_id: '5bc56a4ee9b43a9df8491',group_name: 'Test Group'
            }
          }
        }
      ],params: {
        container_role: 'product_list',show_container_title: false,title: 'my fancy title',actions: [{type: 'openProduct',params: {id: '123'}}]
      }
    };

    renderComponentWithParams(container);
    fixture.detectChanges();

    const elem = element.querySelector('.container-action-tile');
    expect(elem).not.toBeNull();

    const actionsService = Testbed.inject(ActionsService);
    spyOn(actionsService,'performActions');


    elem.click();
    fixture.detectChanges();
    expect(actionsService.performActions).toHaveBeenCalledWith(
      [{type: 'openProducts',params: {query_params: {filtered_group_id: '5bc56a4ee9b4351aa9df8491'},title: 'Test Group'}}]
    );
  });
});

我在运行测试时遇到的错误

Cannot read property utils of undefined

enter image description here

解决方法

您必须模拟 window 对象才能拥有您想要的东西。

function renderComponentWithParams(params) {
    fixture = TestBed.createComponent(ContainerHeaderComponent);
    component = fixture.componentInstance;
    
    window.ChWidget = {};
    window.ChWidget.utils = {};
    window.chWidget.utils.getQueryStringParam = () => 'the id you want';

    setComponentParams(params);
    fixture.detectChanges();
    compiled = fixture.debugElement.nativeElement;
  }

it('renders the show all if there is an action on the container and opens a product group page when clicking it') {
  ...
}

afterEach(() => {
  // Delete the ChWidget on the window because it will leak between tests.
  // Every test,you should mock it again.
  delete window.ChWidget;
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?