[英]How to call a helper function from Cypress?
I am testing a component, that depends on a service to provide an object:我正在测试一个组件,它依赖于提供 object 的服务:
export class MyComponent {
public constructor { private myObjectService: ObjectService }
public ngOnInit() {
this.functionThatNeedsObject();
}
private functionThatNeedsObject() {
this.myObjectService.getObject();
}
}
This object will always initially be null
.这个 object 最初总是
null
。 To have my tests succeed, it should be initialized by the service:为了让我的测试成功,它应该由服务初始化:
export class ObjectService {
private NeededObject myObject;
public getObject() {
... return object
}
public setObject() {
... this function needs to be called, so that myObject is not null
}
}
My test checks wether MyComponent is shown, which will only happen when MyObject is not null.我的测试检查是否显示了 MyComponent,这只会在 MyObject 不是 null 时发生。 Something like:
就像是:
When('I receive data from my services', () => {
cy.get('my-component-selector').should('be.visible')
})
The object in this case is a Behaviorsubject that will normally be set by a service that is not available while testing, and so I need to manually call myObjectService.setObject()
programatically in my Cypress test.在这种情况下,object 是一个 Behaviorsubject,通常由测试时不可用的服务设置,因此我需要在赛普拉斯测试中以编程方式手动调用
myObjectService.setObject()
。
Is this possible?这可能吗?
Did you tried inject
?你试过
inject
吗? Also to test visibility you may need to access the dom using nativeElement
此外,为了测试可见性,您可能需要使用
nativeElement
访问 dom
When('I receive data from my services', inject([ServiceName],(serviceName:ServiceName) => { // here you can set the data using serviceName // then call the component. cy.get('my-component-selector').should('be.visible') })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.