[英]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.