Primeng确认服务的角度单位测试

发布于 2025-02-09 11:46:37 字数 1812 浏览 1 评论 0原文

我无法弄清楚如何在此功能中测试接受和拒绝功能:

confirmReplan(event: any) {
    this.confirmationService.confirm({
      target: event.target,
      key: 'replan',
      message: 'lorem',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        this.messageService.add(/** **/);
        this.replanRequest();
        this.displayReplan = false;
      },
      reject: (type: any) => {
        this.rejectMessage(type);
      }
    });
  }

查看类似的问题,我尝试了许多解决方案。似乎对他人有用的是这种风格:

let component: RequestsTableComponent;
let fixture: ComponentFixture<RequestsTableComponent>;
let confirmationServiceSpy: ConfirmationService;
let requestServiceSpy: RequestApiService;
let messageServiceSpy: MessageService;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [RequestsTableComponent],
      imports: [HttpClientTestingModule],
      providers: [MessageService, RequestApiService, ConfirmationService]
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(RequestsTableComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    messageServiceSpy = TestBed.inject(MessageService);
    requestServiceSpy = TestBed.inject(RequestApiService);
    confirmationServiceSpy = TestBed.inject(ConfirmationService);
  });

  it('/**/', async() => {
    spyOn(confirmationServiceSpy, 'confirm').and.callFake((confirmation: any) => {return confirmation.accept()});

    component.confirmReplan({ target: undefined });
    expect(confirmationServiceSpy.confirm).toHaveBeenCalled();
  });

但是间谍从未被召唤。

jasmine coverage Reporter Reporter 代码覆盖范围报告

I cannot figure out how to test accept and reject functions inside this function:

confirmReplan(event: any) {
    this.confirmationService.confirm({
      target: event.target,
      key: 'replan',
      message: 'lorem',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        this.messageService.add(/** **/);
        this.replanRequest();
        this.displayReplan = false;
      },
      reject: (type: any) => {
        this.rejectMessage(type);
      }
    });
  }

Looking at similiar questions i tried many solutions. One that seems to work for others is of this style:

let component: RequestsTableComponent;
let fixture: ComponentFixture<RequestsTableComponent>;
let confirmationServiceSpy: ConfirmationService;
let requestServiceSpy: RequestApiService;
let messageServiceSpy: MessageService;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [RequestsTableComponent],
      imports: [HttpClientTestingModule],
      providers: [MessageService, RequestApiService, ConfirmationService]
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(RequestsTableComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    messageServiceSpy = TestBed.inject(MessageService);
    requestServiceSpy = TestBed.inject(RequestApiService);
    confirmationServiceSpy = TestBed.inject(ConfirmationService);
  });

  it('/**/', async() => {
    spyOn(confirmationServiceSpy, 'confirm').and.callFake((confirmation: any) => {return confirmation.accept()});

    component.confirmReplan({ target: undefined });
    expect(confirmationServiceSpy.confirm).toHaveBeenCalled();
  });

but the spy is never called.

Code coverage report from jasmine coverage reporter

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

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

发布评论

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

评论(1

迎风吟唱 2025-02-16 11:46:37

一种方法是对参数进行处理,并将conve> Accept手动拒绝

it('/**/', async() => {
    // associate this spy to a variable so we have a handle on it later
    const confirmSpy = spyOn(confirmationServiceSpy, 'confirm');

    component.confirmReplan({ target: undefined });
    // get a handle on the most recent argument to this spy
    const arg = confirmSpy.calls.mostRecent().args[0];
    // call accept
    arg.accept();
    // call reject if you like
    arg.reject();
    expect(component.displayReplan).toBeFalse();
    // now they should be covered
  });

One way is to have a handle on the argument and call the accept and reject manually.

it('/**/', async() => {
    // associate this spy to a variable so we have a handle on it later
    const confirmSpy = spyOn(confirmationServiceSpy, 'confirm');

    component.confirmReplan({ target: undefined });
    // get a handle on the most recent argument to this spy
    const arg = confirmSpy.calls.mostRecent().args[0];
    // call accept
    arg.accept();
    // call reject if you like
    arg.reject();
    expect(component.displayReplan).toBeFalse();
    // now they should be covered
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文