我需要在我的 Angular Store 中注入一个值(调度一个操作),以使我的 Cypress e2e 测试正常工作。事实上,我需要我的商店包含 { isLoggedIn : true } 来访问应用程序中的任何页面,否则用户将被重定向到登录页面。
我在这里发现了类似的问题Cypress - 以编程方式操作 Angular/NGRX 应用程序 https://stackoverflow.com/questions/57542554/cypress-programmatically-manipulating-angular-ngrx-app,但答案似乎不适用于我的情况。
我总是收到错误:“cy.its() 等待指定的属性存储存在,但它从未存在。”
运行测试时,cypress 的控制台中没有打印任何console.log。
这是我在 app.component.ts 中的代码
export interface IWindowCypress {
store: Store;
Cypress: unknown;
}
@Component({
selector: 'my-root',
templateUrl: './app.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
constructor(protected store: Store) {
const windowWithStore: IWindowCypress = window as unknown as IWindowCypress;
console.log('AppComponent');
if (windowWithStore.Cypress) {
console.log('save store');
windowWithStore.store = store;
}
}
}
这是我的柏树测试
describe('ContactListComponent', () => {
before(() => {
cy.window().its('store').invoke('dispatch', { type: '[User Effect] Load user success', user: { isLoggedIn : true } });
});
// some tests
所以我想知道我在这里错过了什么或者我做错了什么。有人可以帮忙吗?谢谢 !
PS:如果我运行我的应用程序npm run start
我在控制台中打印了“AppComponent”
您的代码和参考问题之间的区别是
window.store = this.store; // referring to component instance store property
我不知道为什么注射store
不工作,但尝试
export class AppComponent {
constructor(protected store: Store) {
const windowWithStore: IWindowCypress = window as unknown as IWindowCypress;
if (windowWithStore.Cypress) {
windowWithStore.store = this.store;
}
}
}
您可能还希望接口属性是可选的,因为它们并不总是存在。
export interface IWindowCypress {
store?: Store;
Cypress?: unknown;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)