简体   繁体   English

使用赛普拉斯时如何在Angular中模拟或存根函数?

[英]how do I mock or stub a function in Angular when using Cypress?

Im using Cypress.io as part of the way that Im testing an Angular 7 app. 我使用Cypress.io作为Im测试Angular 7应用程序的一部分。 I've combed through the docs, forums and blogs, but I dont see a way to mock or stub a function in Angular. 我已经浏览了文档,论坛和博客,但没有看到在Angular中模拟或存根函数的方法。 Im trying to mock the employee Observable. 我试图嘲笑员工可观察。 I need to have the Employee array contain an employee object so that this.federalTaxesService.loadFederalTaxes() function is called. 我需要让Employee数组包含一个employee对象,以便this.federalTaxesService.loadFederalTaxes()函数。 In production, this app gets the Employee array from the NgRx store, but the store is not working correctly when using Cypress for some reason. 在生产中,此应用程序从NgRx商店获取Employee数组,但由于某些原因,在使用赛普拉斯时该商店无法正常工作。

export class EmployeeTaxDetailsContainerComponent implements OnInit, OnDestroy {
  watchEmployee$: Subscription;
  watchFederalTaxes$: Subscription;

  federalTaxes$: Observable<any> = this.federalTaxesService.federalTaxes$;
  employees$: Observable<any> = this.employeesService.employees$;

  federalTaxes: FederalTaxes;
  employeeUuid: string;

  constructor(public employeesService: EmployeesFacade, public federalTaxesService: FederalTaxesFacade) {}

  ngOnInit() {
    this.watchEmployee$ = this.employees$.subscribe((employees: Employee[]) => {
      if (employees && employees.length > 0) {
        this.employeeUuid = employees[0].uuid;
        this.federalTaxesService.loadFederalTaxes(this.employeeUuid);
      }
    });

    this.watchFederalTaxes$ = this.federalTaxes$.subscribe((federalTaxes: FederalTaxes) => {
      if (federalTaxes) {
        this.federalTaxes = federalTaxes;
      }
    });
  }

  ngOnDestroy() {
    if (this.watchFederalTaxes$) {
      this.watchFederalTaxes$.unsubscribe();
    }
    if (this.watchEmployee$) {
      this.watchEmployee$.unsubscribe();
    }
  }
}

I didnt end up trying to mock the Observable. 我最终没有尝试嘲笑Observable。 Most of that code is covered in my unit tests. 我的单元测试中涵盖了大部分代码。 What I really needed was to have this.federalTaxes have a value, which is what the code below does. 我真正需要的是让this.federalTaxes具有一个值,这就是下面的代码所要做的。

cy.get('.some-ng-element').then(($el) => {
    const el = $el[0].parentElement;
    const win = el.ownerDocument.defaultView;
    const component = win.ng.probe(el).componentInstance;
    component.federalTaxes = { additionalWithholding: 22299, allowances: 1, filingStatus: 'Single' };
});

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

相关问题 我需要创建一个模拟/存根来测试该异步功能吗? - Do I need to create a mock/stub to test this asynchronous function? 在 Angular 10+ 中测试具有 ViewChildren 的组件时,如何使用假/模拟/存根子组件? - How can I use a fake/mock/stub child component when testing a component that has ViewChildren in Angular 10+? Angular Integration测试:测试发出其他组件的事件时调用的函数,如何模拟事件数据? - Angular Integration test: testing a function that is called when event from other component is emitted, how do I mock the event data? Angular 单元测试 - 使用 Stub 模拟子组件 - Angular Unit Testing - Mock Child Component using Stub 如何在 Angular 中模拟 HTTP 调用? - How do I mock HTTP calls in Angular? 我如何模拟Angular 2路线? - How do I Mock an Angular 2 route? 将Jasmine与Ionic结合使用是否可以对静态名称空间函数调用进行模拟/存根? - Using Jasmine with Ionic is there a way to mock/stub a static namespace function call? 在 cypress 测试中模拟/存根/忽略谷歌 recaptcha v3 请求 - mock/stub/ignore google recaptcha v3 requests on cypress tests 使用赛普拉斯时,为什么需要显式清除sessionStorage? - Why do I need to explicitly clear sessionStorage when using Cypress? 如何在不使用TestBed的情况下在Angular中模拟http发布? - how do I mock out http post in Angular without using TestBed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM