Angular NgRx效果:单元测试-似乎动作$永远不会产生效果

Angular NgRx效果:单元测试-似乎动作$永远不会产生效果,angular,unit-testing,ngrx,ngrx-effects,Angular,Unit Testing,Ngrx,Ngrx Effects,当运行我的单元测试时,我注意到Jasmine记录了一条消息,说我的单元测试没有期望。因此,我注意到单元测试没有很好地工作。看来这一行动并没有触发效果。我应该提到UI让国家没有问题 我正在基于和编写代码 影响 @Injectable() 出口类国家效应{ 构造函数(私有操作$:操作, 私人countryService:countryService, 私人商店:商店){} loadCountries$=createEffect(()=> 此.actions$管道( ofType(CountryAct

当运行我的单元测试时,我注意到Jasmine记录了一条消息,说我的单元测试没有期望。因此,我注意到单元测试没有很好地工作。看来这一行动并没有触发效果。我应该提到UI让国家没有问题

我正在基于和编写代码

影响

@Injectable()
出口类国家效应{
构造函数(私有操作$:操作,
私人countryService:countryService,
私人商店:商店){}
loadCountries$=createEffect(()=>
此.actions$管道(
ofType(CountryActions.ActionTypes.LOAD_COUNTRIES),
withLatestFrom(this.store.select(selectCountries)),
合并地图([action,storeCountries])=>{
如果(storeCountries&&storeCountries.length>0){
返回(CountryActions.LoadCountriesAccess({countries:storeCountries}))
}
返回此.countryService.getAll()管道(
map((apiccountries:Country[])=>CountryActions.LoadCountriesAccess({countries:apiccountries})),
catchError(err=>
of(CountryActions.loadCountriesFailure({错误:“无法加载国家/地区”,国家/地区:null}))
)
)
})
)
);
}
效果规格:

从“rxjs”导入{of,Observable};
从“@angular/core/testing”导入{TestBed};
从“@ngrx/effects/testing”导入{provideMockActions};
从'@ngrx/store/testing'导入{provideMockStore};
从'src/app/vendors/shared/services/country.service'导入{CountryService};
从“/country.effects”导入{CountryEffects};
从'src/test/mocks/country.mock'导入{COUNTRIES};
从'@ngrx/store'导入{Action};
从“”导入{CountryActions};
描述('CountryEffects',()=>{
让动作$:可观察;
let效应:国家效应;
让服务:jasmine.SpyObj;
在每个之前(()=>{
TestBed.configureTestingModule({
供应商:[
国家效应,
provideMockActions(()=>actions$),
provideMockStore({
选择器:[
{
选择器:CountryActions.ActionTypes.LOAD\u国家/地区,
价值:{
国家:[]
}
}
]
}),
{ 
提供:乡村服务,
useValue:jasmine.createSpyObj('countryService',['getAll']))
},
]
});
effects=TestBed.get(CountryEffects);
service=TestBed.get(CountryService);
});
它('loadCountries$应返回CountryAction.loadCountriesSuccess',()=>{
actions$=的({type:CountryActions.ActionTypes.LOAD_COUNTRIES});
服务.getAll.and.returnValue(国家的);
effects.loadCountries$.subscribe(操作=>{
//这就是我期望它失败的地方,因为效果应该会回来
//键入ActionTypes.LOAD\u COUNTRIES\u成功
expect(action.type).toEqual('Please fail');
期望(行动。国家)。toEqual(国家);
});
});
});

不要使用的来创建操作$,而是尝试创建可观察的热,如下所示

 actions$ = hot('-a--', {
    a: { type: CountryActions.ActionTypes.LOAD_COUNTRIES },
  });

最后,我所做的就是更改
provideMockStore
以使用简单的
initialState
。它包含状态的所有属性,设置为您需要的任何默认值

providers: [
    CountryEffects,
    provideMockActions(() => actions$),
    provideMockStore({ initialState }),
    {
        provide: CountryService,
        useValue: jasmine.createSpyObj('countryService', ['getAll'])
        },
]


谢谢,但我希望与文档示例保持一致,避免在我的TET中使用热/冷