jasmine - 如何设置新状态来测试可观察对象?
问题描述
我整天都在尝试用茉莉花做一个简单的测试,但我认为我做错了什么。我有一段代码要测试,但我不能进去。我试图遵循 nrgx 7 文档,但我失败了。
下面的单元测试应该测试我的 enderecoFeatureSubscription。store.setState({ cep: null, endereco: RES }) 对商店没有做任何事情,所以我的订阅没有做任何事情
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let store: MockStore<ICepState>
const initialState = {
cep: null, endereco: null
};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FormComponent],
imports: [StoreModule.forRoot({}),],
providers: [
provideMockStore({ initialState: CEPSTATE })
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
store = TestBed.get(Store);
});
it('should test enderecoFeatureSubscription ', () => {
store.setState({ cep: null, endereco: RES })
expect(component.endereco).toEqual(RES)
});
零件
private enderecoFeatureSubscription = this.store.pipe(select(enderecoFeatureSelector)).subscribe((endereco: IEndereco | any) => {
if (!endereco) {
return;
}
this.endereco = endereco
})
如果你能帮助我谢谢你,因为我已经浪费了很多时间。
解决方案
在 ngrx 版本中。> 8.0.0,如果您在相应的覆盖选择器上使用,则有一种方法会store.refreshState
刷新状态。store.setState
不幸的是,refreshState
ngrx 7 中不存在方法。还有一种替代方法 - 您应该使用store.overrideSelector
这样的方法覆盖所需的选择器 -
it('should test enderecoFeatureSubscription ', () => {
store.overrideSelector(enderecoFeatureSelector, <put you mocked value>
fixture.detectChanges(); //MAKE sure to remove fixture.detectChanges() from beforeEach
expect(component.endereco).toEqual(RES)
});
推荐阅读
- angular - Angular中组件之间的通信
- c# - TypeError:Blazor webassembly 解决方案中的 FailedToFetch
- javascript - JSON 多维数组到 HTML 表
- python - python中列的所有字符串的间距相等
- python - Instagram 帖子抓取
- nuxt.js - 使用 nuxtjs api 从strapi 中恢复内容
- mysql - 在 laravel 中迁移 PROCEDURE
- xamarin - GeoCoding GetLocationAsync 方法返回 Xamarin Forms IOS 中特定地址的 KLCErrorDomain Code = 8 错误
- javascript - Console.log(appointment[0].apps.date) 给出了一个日期,但是当我在我的函数中使用它时,我得到'无法读取未定义的属性'日期''
- c# - 文件句柄 Asp.Net 中的等待问题