角度排除外部JavaScript库中的单位测试

发布于 2025-01-24 02:36:24 字数 1273 浏览 4 评论 0原文

我正在使用使用外部JavaScript库的组件来测试我的组件,如果我评论行 aksservice = aksserviceFactory(),我的所有测试用例都可以在我的组件中导入和初始化。 (下面的代码),但它给出了一个错误 typeError:如果我不计入它,则无法读取未定义的属性(读取'aks')。我也尝试在Spec.ts文件中导入库,但会产生相同的错误。

我想在我的component.spec.ts文件中包含此库,这样它就不会给我这个错误,或者如果有一种方法,我们可以将此文件排除在单位测试中,以免给出此错误。

component.ts文件

import { aksServiceFactory } from '../../services/gdl.service';
import { GlobalConstants } from '../../global-constants';


@Component({
  selector: 'app-page',
  templateUrl: './opt-page.component.html',
  styleUrls: ['./opt-page.component.scss']

})

export class OptPageComponent implements OnInit {

  //initialize aks Service

  aksService = aksServiceFactory();

aks.service.ts

// aks.service.js

export const aksServiceFactory = () => {
    const aksQueue = window.cms.aks;
    const sendPageview = (pageId) => {
      aksQueue.push(['event:publish', ['page', 'pageinfo']]);
    };

    const sendEvent = (category, name, payload) => {
      aksQueue.push(['event:publish', [category, name, payload]]);
    };

    const enableDebugMode=()=>{
      aksQueue.debug.enable();
    }

    return {
      enableDebugMode,
      sendPageview,
      sendEvent,
    };
  };

I am unit testing my component which uses a external javascript library, I am importing and initializing the libary in my component.ts All my test cases work fine if I comment the line aksService = aksServiceFactory(); (code below) but it gives a error TypeError: Cannot read properties of undefined (reading 'AKS') if i uncomment it. I tried importing the library in spec.ts file also but it gives same error.

I want to include this library in my component.spec.ts file so it doesnt give me this error or if there is a way can we exclude this file from unit test so it doesnt give this error.

component.ts file

import { aksServiceFactory } from '../../services/gdl.service';
import { GlobalConstants } from '../../global-constants';


@Component({
  selector: 'app-page',
  templateUrl: './opt-page.component.html',
  styleUrls: ['./opt-page.component.scss']

})

export class OptPageComponent implements OnInit {

  //initialize aks Service

  aksService = aksServiceFactory();

aks.service.ts

// aks.service.js

export const aksServiceFactory = () => {
    const aksQueue = window.cms.aks;
    const sendPageview = (pageId) => {
      aksQueue.push(['event:publish', ['page', 'pageinfo']]);
    };

    const sendEvent = (category, name, payload) => {
      aksQueue.push(['event:publish', [category, name, payload]]);
    };

    const enableDebugMode=()=>{
      aksQueue.debug.enable();
    }

    return {
      enableDebugMode,
      sendPageview,
      sendEvent,
    };
  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2025-01-31 02:36:24

用依赖注入创建服务并模拟它。单元测试不应仅在必要时才依靠外部LIB,必须测试组件的功能,因此,当您模拟它时,必须使用正确的返回值返回模拟的服务。这将使图书馆如果更改单元测试将不会失败。 :)
这些文章将为您提供帮助:

如何模拟单位测试的角组件中的服务功能

https:// https:// angular.io/guide/dependenty-invoction-providers#using-factory-Providers

Create the service with dependency injection and mock it. The unit test should not rely on external libs only if it is neccessary you have to test the functionality of the component, so when you mock it you have to return the mocked service with the correct return values. This will garantee that if the library change the unit test will not fail. :)
These articles will help to you:

How to mock service function in Angular component for unit test

https://angular.io/guide/dependency-injection-providers#using-factory-providers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文