测试带有匿名函数内部匿名功能的ADDListener -Angular Jasmine(隔离测试)

发布于 2025-02-13 14:40:29 字数 2212 浏览 0 评论 0原文

最近有人帮助我进行了功能测试,但是我无法访问该功能的一部分,因为它是AddListener中的匿名功能。我阅读了文档,但我找不到任何内容。可以访问这些区域吗?

这是我的组件代码

import { EventEmitter } from "events";

@Component({
  selector: "app-mug",
  templateUrl: "./mug.component.html",
  styleUrls: ["./mug.component.scss"],
})
export class MugComponent implements OnInit
{
  @Output() nav: EventEmitter<number> = new EventEmitter<number>();

  public mug: Mug= new Mug();

  constructor(
    public constants: Constants,
    public mugService: mugService,
    public basicService: BasicService,
    public toolsService: ToolsService,
    private modalService: NgbModal,
  ) 
  
  ngOnInit() {
    this.basicService.siteChange.subscribe((value) => {
      this.mugService.filterC.id = value;
      this.mugService.getRelatedMugs(); 
    });
  }

  public openModal(contentType: string): void {
    this.toolsService.eventEmitter.removeAllListeners();
    this.toolsService.eventEmitter.addListener("close-modal", () =>
      this.toolsService.eventEmitter.removeAllListeners()
    );
    this.toolsService.eventEmitter.addListener(
      "contentEvent",
      (content) => {
        this.setContent(contentType, content);
      }
    );
    const data = {
      component: ModalSearchComponent,
      input: inputList,
      ModalTitle: "Mug finder",
    };
    const modalC = this.modalService.open(ModalContainerComponent, {
      size: "lg",
      backdrop: "static",
    });
    modalC.componentInstance.showComponent(data);
  }
}

let modalService = jasmine.createSpyObj("modalService", ["open"]);
modalService.open.and.returnValue({
  componenntInstance: {
     showComponent: () => null,
  }
});
let toolsService = jasmine.createSpyObj("toolsService", ["toRoute"], {
    eventEmitter: {
      addListener: () => null,
      removeAllListeners: () => null,
    }
  });
....
component = new MugComponent(
      constants,
      mugService,
      basicService,
      toolsService,
      modalService,
    );

这是我的规格。

.net/wwa4l.png“ rel =“ nofollow noreferrer”> “在此处输入图像说明”

Someone here recently helped me with a function test, but I can't access a part of that function because it's an anonymous function inside an addListener.I read the documentation but I can't find anything about this. Is it possible to access these areas?

This is my component code

import { EventEmitter } from "events";

@Component({
  selector: "app-mug",
  templateUrl: "./mug.component.html",
  styleUrls: ["./mug.component.scss"],
})
export class MugComponent implements OnInit
{
  @Output() nav: EventEmitter<number> = new EventEmitter<number>();

  public mug: Mug= new Mug();

  constructor(
    public constants: Constants,
    public mugService: mugService,
    public basicService: BasicService,
    public toolsService: ToolsService,
    private modalService: NgbModal,
  ) 
  
  ngOnInit() {
    this.basicService.siteChange.subscribe((value) => {
      this.mugService.filterC.id = value;
      this.mugService.getRelatedMugs(); 
    });
  }

  public openModal(contentType: string): void {
    this.toolsService.eventEmitter.removeAllListeners();
    this.toolsService.eventEmitter.addListener("close-modal", () =>
      this.toolsService.eventEmitter.removeAllListeners()
    );
    this.toolsService.eventEmitter.addListener(
      "contentEvent",
      (content) => {
        this.setContent(contentType, content);
      }
    );
    const data = {
      component: ModalSearchComponent,
      input: inputList,
      ModalTitle: "Mug finder",
    };
    const modalC = this.modalService.open(ModalContainerComponent, {
      size: "lg",
      backdrop: "static",
    });
    modalC.componentInstance.showComponent(data);
  }
}

This is my Spec.ts code

let modalService = jasmine.createSpyObj("modalService", ["open"]);
modalService.open.and.returnValue({
  componenntInstance: {
     showComponent: () => null,
  }
});
let toolsService = jasmine.createSpyObj("toolsService", ["toRoute"], {
    eventEmitter: {
      addListener: () => null,
      removeAllListeners: () => null,
    }
  });
....
component = new MugComponent(
      constants,
      mugService,
      basicService,
      toolsService,
      modalService,
    );

This is my code coverage

enter image description here

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

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

发布评论

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

评论(1

如若梦似彩虹 2025-02-20 14:40:29

是的,应该尝试一下:

// !! this will get the most recent call for `addListener` and get a handle on the arguments
// !! the first element in the array is the stringArgument ("close-modal", "contentEvent") and the 2nd element is the function for the call.
const [stringArgument, function] = toolsService.addListener.calls.mostRecent().args;

以上将为您提供最后一次(mostrecent())addlistener的调用,但是如果您想对所有调用进行操作,call我认为应该有所有的电话。

Yes, it should be, try this:

// !! this will get the most recent call for `addListener` and get a handle on the arguments
// !! the first element in the array is the stringArgument ("close-modal", "contentEvent") and the 2nd element is the function for the call.
const [stringArgument, function] = toolsService.addListener.calls.mostRecent().args;

The above will give you the last time (mostRecent()) addListener was called but if you want to have a handle of all of the calls, calls should have all of the calls I think.

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