gpt4 book ai didi

angular - 如何在 Angular 分量中测试 NGXS?

转载 作者:行者123 更新时间:2023-12-02 14:47:44 29 4
gpt4 key购买 nike

我是 NGXS 的新手,在测试时我在所有测试用例中都遇到了错误。我正在从商店获取数据:app.component.ts

    somedata: Idetail
this.someData = this.store.selectSnapshot(State.details).
this.data = this.someData.DATA;

但是我在每个测试用例上都出错了:

TypeError: Cannot read property 'DATA' of null

最佳答案

您必须模拟商店才能在测试中使用它。

例如:

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent]
imports: [NgxsModule.forRoot([])] // import real module without state
});

const store:Store = TestBed.get(Store);
spyOn(store, 'select').and.returnValue(of(null)); // be sure to mock the implementation here
spyOn(store, 'selectSnapshot').and.returnValue(null); // same here
}));

关于angular - 如何在 Angular 分量中测试 NGXS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57990216/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com