Angular 如何在角分量中测试NGX?

Angular 如何在角分量中测试NGX?,angular,ngxs,Angular,Ngxs,我是NGXS新手,在测试过程中,我在所有测试用例中都遇到了错误。 我正在从存储区获取数据: app.component.ts 但我在每一个测试用例中都会遇到错误: TypeError:无法读取null的属性“数据” 如果要将数据从存储区获取到用于单元测试的组件中,请执行以下操作: 在TestBed.configuretedtingmodule中,将添加到导入NgxsModule.forRoot[StateName] 确保组件具有正确的选择器设置以获取数据 在单元测试用例中,获取存储实例:cons

我是NGXS新手,在测试过程中,我在所有测试用例中都遇到了错误。 我正在从存储区获取数据: app.component.ts

但我在每一个测试用例中都会遇到错误:

TypeError:无法读取null的属性“数据”


如果要将数据从存储区获取到用于单元测试的组件中,请执行以下操作:

在TestBed.configuretedtingmodule中,将添加到导入NgxsModule.forRoot[StateName]

确保组件具有正确的选择器设置以获取数据

在单元测试用例中,获取存储实例:conststore:Store=TestBed.getStore

来自测试用例存储的调度存储操作。dispatchnew AddToDotoDo

检查组件上的响应
有关更多信息,请参阅我在4周前开始使用NGXS时发现非常有用的文档:

如果您想将数据从存储区获取到组件中进行单元测试,请执行以下操作:

在TestBed.configuretedtingmodule中,将添加到导入NgxsModule.forRoot[StateName]

确保组件具有正确的选择器设置以获取数据

在单元测试用例中,获取存储实例:conststore:Store=TestBed.getStore

来自测试用例存储的调度存储操作。dispatchnew AddToDotoDo

检查组件上的响应
有关更多信息,请访问我在4周前开始使用NGXS时发现非常有用的文档:

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

例如:


您必须模拟存储才能在测试中使用它

例如:


如果你能提供更多的代码和/或stackblitz,那么我想你会找到更多的帮助。

如果你能提供更多的代码和/或stackblitz,那么我想你会找到更多的帮助。

这似乎是一个奇怪的话题,但希望我能给出一些关于使用角度组件进行NGX单元测试的提示:

导入NgxsModule.forRoot[StateName1,StateName2,…],然后添加StateName1和StateName2中的每个构造函数注入。。。在TestBed.configureTestingModule提供程序下,确保您已经为每次注入准备了适当的模拟,这一点非常重要

以下是我的例子:

TestBed.configureTestingModule({
      declarations: [MyComponent]
      imports: [NgxsModule.forRoot([NAME_OF_YOUR_TESTING_STATE])]
   });

您应该事先定义存储和操作$object

我可以通过以下方式实现:

it('should segmentButtonChanged', () => {
        let actionDispatched = false;
        zip(actions$.pipe(ofActionDispatched(ChangeButton))).subscribe(() => (actionDispatched = true));

        const mockedString = 'hello there';
        component.changeButton(mockedString);

        expect(actionDispatched).toBeTruthy();
        expect(component.isButtonChanged).toBeTruthy();
}); 

这似乎是一个奇怪的话题,但希望我能给出一些关于使用角度组件进行NGX单元测试的技巧:

导入NgxsModule.forRoot[StateName1,StateName2,…],然后添加StateName1和StateName2中的每个构造函数注入。。。在TestBed.configureTestingModule提供程序下,确保您已经为每次注入准备了适当的模拟,这一点非常重要

以下是我的例子:

TestBed.configureTestingModule({
      declarations: [MyComponent]
      imports: [NgxsModule.forRoot([NAME_OF_YOUR_TESTING_STATE])]
   });

您应该事先定义存储和操作$object

我可以通过以下方式实现:

it('should segmentButtonChanged', () => {
        let actionDispatched = false;
        zip(actions$.pipe(ofActionDispatched(ChangeButton))).subscribe(() => (actionDispatched = true));

        const mockedString = 'hello there';
        component.changeButton(mockedString);

        expect(actionDispatched).toBeTruthy();
        expect(component.isButtonChanged).toBeTruthy();
}); 

我尝试了此操作,但出现了错误:TypeError:无法读取UnfinedWorked for service的属性“ngInputableDef”,但在Component中出现了相同的错误。您能帮助我解决此问题吗?谢谢我尝试了此操作,但出现了错误:TypeError:无法读取UnfinedWorked for service的属性“ngInputableDef”,但在Component中出现了相同的错误。您能帮助我解决此问题吗?谢谢有没有办法避免导入服务,甚至是模拟服务?因为我有很多需要模拟的服务。@rosiejaneenomar不幸的是,我到目前为止还没有解决方案,但是导入所有服务有没有办法避免导入服务甚至是模拟服务?因为我有很多需要模仿的服务。@rosiejaneenomar不幸的是,我到目前为止没有解决方案,只能导入所有的服务
let store: Store;
let actions$: Observable<any>;
public changeButton(value: string): void {
       this.store.dispatch(new ButtonChange(value));
       this.isButtonChanged = true;
}
it('should segmentButtonChanged', () => {
        let actionDispatched = false;
        zip(actions$.pipe(ofActionDispatched(ChangeButton))).subscribe(() => (actionDispatched = true));

        const mockedString = 'hello there';
        component.changeButton(mockedString);

        expect(actionDispatched).toBeTruthy();
        expect(component.isButtonChanged).toBeTruthy();
});