简体   繁体   English

如何让我的Angular5 spyOn按预期工作?

[英]How can I get my Angular5 spyOn to work as expected?

My test is not detecting changes. 我的测试没有检测到变化。 Here is my component: 这是我的组件:

  toggleUploadModal() {
    const modalRef = this.ngbModal.open(UploadFilesComponent, { size: 'lg', backdrop: 'static' });
    modalRef.componentInstance.DeliverableTransaction = this.transactionDetails;
    modalRef.result.then((res) => {
      if (res.status === 'success') {
        this.deliverableTransactionService.updateDeliverableTransaction(this.route.snapshot.params.id, {
          submissionStatus: 'In Process'
        })
      }
      setTimeout(() => {
        this.uploadStatus = {};
      }, 5000);
    })
  }

My test has: 我的测试有:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [TransactionFileViewer, NgxPermissionsAllowStubDirective],
      providers: [...],
      imports: [...],
      schemas: [
        NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
      ],
    })
      .compileComponents();
    fixture = TestBed.createComponent(TransactionFileViewer);
    downloadService = TestBed.get(DownloadService);
    component = fixture.componentInstance;
    fixture.detectChanges();
    submissionFileService = TestBed.get(SubmissionFileService);
    deliverableDefinitionService = TestBed.get(DeliverableDefinitionService);
    service = TestBed.get(DeliverableDefinitionDetailViewService);
    deliverableTransactionService = TestBed.get(DeliverableTransactionService);
    modalService = TestBed.get(NgbModal);
    flashMessagesService = TestBed.get(FlashMessagesService);
  }));
  fit('should update the submissionStatus upon file upload', () => {
    spyOn(modalService, 'open').and.returnValue({
      componentInstance: {},
      result: Promise.resolve({
        uploadedFileCount: 5,
        status: 'success'
      })
    });
    spyOn(deliverableTransactionService, 'updateDeliverableTransaction').and.returnValue(true);

    component.toggleUploadModal();
    expect(deliverableTransactionService.updateDeliverableTransaction).toHaveBeenCalledWith(1, {
      submissionStatus: 'In Process'
    });
  })

However, the updateDeliverableTransaction never is called in the test. 但是,在测试中永远不会调用updateDeliverableTransaction What am I doing wrong? 我究竟做错了什么? I assume I need to somehow bind the scope to the result , but I'm unsure how. 我假设我需要以某种方式将范围绑定到result ,但我不确定如何。 I'm using bluebird if it matters. 如果重要的话,我正在使用bluebird

updateDeliverableTransaction method will not call because it's in success callback of modal. updateDeliverableTransaction方法不会调用因为它成功回调模态。 You need to add fixture.detectChanges() after your method call. 您需要在方法调用后添加fixture.detectChanges()

Try this 试试这个

fit('should update the submissionStatus upon file upload', () => {
    spyOn(modalService, 'open').and.returnValue({
      componentInstance: {},
      result: Promise.resolve({
        uploadedFileCount: 5,
        status: 'success'
      })
    });
    spyOn(deliverableTransactionService, 'updateDeliverableTransaction').and.returnValue(true);

    component.toggleUploadModal();
    fixture.detectChanges();
    expect(deliverableTransactionService.updateDeliverableTransaction).toHaveBeenCalledWith(1, {
      submissionStatus: 'In Process'
    });
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使源地图在使用webpack和angular5的jhipster应用程序的chrome中工作? - How do I get the source maps to work in chrome for jhipster app that uses webpack and angular5? 我在angular5中看不到表单中的输入值 - I can't see input value in my form in angular5 如何在Angular5效果中从回调返回? - How can I return from a callback in an Angular5 effect? 如何在angular5中动态链接到外部站点? - How can I make dynamic url to external site in angular5? 我如何检查angular5中的所有mat-checkbox? - How can i check all mat-checkbox in angular5? Angular-Testing:Angular5 spyOn在子组件中不起作用 - Angular-Testing: Angular5 spyOn not working in child components 我从数据库中删除了一个数据行,但从我的网页中删除了一个数据行,直到刷新它。 我如何立即从Angular5中的表中删除该行 - I delete a datarow from my database but not from my webpage until i refresh it. How can i delete that row immediately from my table in Angular5 如何在angular5中获取iframe的内容 - How to get the content of an iframe in angular5 如何获取使用 Angular5 下载的文件的名称 - How to get the name of a file downloaded with Angular5 如何使用Angular5获取重定向URL - How to get a redirect URL with Angular5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM