带有订阅的商店选择器的角度单元测试组件
我正在使用NGRX扇区从商店获取数据。这是我的代码。
组件文件
export class SpinnerComponent implements OnInit {
isLoading = false;
constructor(private readonly store$: Store<AppState>) { }
ngOnInit(): void {
this.store$.pipe(select((state: AppState) => state.spinnerState.isLoading)).
subscribe((data: any) => {
this.isLoading = data;
});
}
规格文件
import { SpinnerComponent } from './spinner.component';
import { AppState } from '../../../core/store/app.state';
import { Store } from '@ngrx/store';
import { provideMockStore } from '@ngrx/store/testing';
fdescribe('SpinnerComponent', () => {
let component: SpinnerComponent;
let fixture: ComponentFixture<SpinnerComponent>;
let store: Store<AppState>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ SpinnerComponent ],
providers: [provideMockStore({
selectors: [
{
selector: 'isLoading',
value: [
{
isLoading: false,
},
],
},
],
})],
})
.compileComponents();
});
beforeEach(() => {
store = TestBed.inject(Store);
spyOn(store, 'select').and.callThrough();
fixture = TestBed.createComponent(SpinnerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('when "ngOnInit()" method calls', () => {
it('should set "isLoading" as false ', () => {
component.ngOnInit();
expect(component.isLoading).toEqual(false);
});
});
});
覆盖范围文件说
这些行不涵盖。 我添加了 spyon(store,'select')。和。callthrough();
我需要在规格中访问store.select()。订阅吗?
编辑1
在 .spec 文件中进行了一些更改
fdescribe('SpinnerComponent', () => {
let component: SpinnerComponent;
let fixture: ComponentFixture<SpinnerComponent>;
let store: MockStore<AppState>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ SpinnerComponent ],
providers: [provideMockStore({
selectors: [
{
selector: SpinnerState.selectSpinnerState,
value: [
{
isLoading: false,
},
],
},
],
})],
})
.compileComponents();
});
beforeEach(() => {
store = TestBed.inject(MockStore);
store.overrideSelector(SpinnerState.selectSpinnerState, false);
spyOn(store, 'select').and.callThrough();
fixture = TestBed.createComponent(SpinnerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('when "ngOnInit()" method calls', () => {
it('should set "isLoading" as false ', () => {
component.ngOnInit();
store.pipe(select(SpinnerState.selectSpinnerState)).subscribe((mockData: any) => {
expect(mockData.isLoading).toBe(false);
});
expect(component.isLoading).toEqual(false);
});
});
});
,现在它在此行上显示remon
expect(mockData.isLoading).toBe(false);
我该如何解决此问题。
请
提前给我建议
Am using using ngrx sectors to get the data from store. Here is my code.
component file
export class SpinnerComponent implements OnInit {
isLoading = false;
constructor(private readonly store$: Store<AppState>) { }
ngOnInit(): void {
this.store$.pipe(select((state: AppState) => state.spinnerState.isLoading)).
subscribe((data: any) => {
this.isLoading = data;
});
}
spec file
import { SpinnerComponent } from './spinner.component';
import { AppState } from '../../../core/store/app.state';
import { Store } from '@ngrx/store';
import { provideMockStore } from '@ngrx/store/testing';
fdescribe('SpinnerComponent', () => {
let component: SpinnerComponent;
let fixture: ComponentFixture<SpinnerComponent>;
let store: Store<AppState>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ SpinnerComponent ],
providers: [provideMockStore({
selectors: [
{
selector: 'isLoading',
value: [
{
isLoading: false,
},
],
},
],
})],
})
.compileComponents();
});
beforeEach(() => {
store = TestBed.inject(Store);
spyOn(store, 'select').and.callThrough();
fixture = TestBed.createComponent(SpinnerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('when "ngOnInit()" method calls', () => {
it('should set "isLoading" as false ', () => {
component.ngOnInit();
expect(component.isLoading).toEqual(false);
});
});
});
The coverage file says
these lines are not covered.
i have added spyOn(store, 'select').and.callThrough();
Do i need to call store.select().subscribe inside the spec?
Edit 1
made some changes in the .spec file
fdescribe('SpinnerComponent', () => {
let component: SpinnerComponent;
let fixture: ComponentFixture<SpinnerComponent>;
let store: MockStore<AppState>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ SpinnerComponent ],
providers: [provideMockStore({
selectors: [
{
selector: SpinnerState.selectSpinnerState,
value: [
{
isLoading: false,
},
],
},
],
})],
})
.compileComponents();
});
beforeEach(() => {
store = TestBed.inject(MockStore);
store.overrideSelector(SpinnerState.selectSpinnerState, false);
spyOn(store, 'select').and.callThrough();
fixture = TestBed.createComponent(SpinnerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('when "ngOnInit()" method calls', () => {
it('should set "isLoading" as false ', () => {
component.ngOnInit();
store.pipe(select(SpinnerState.selectSpinnerState)).subscribe((mockData: any) => {
expect(mockData.isLoading).toBe(false);
});
expect(component.isLoading).toEqual(false);
});
});
});
Now its showing errorn on this line
expect(mockData.isLoading).toBe(false);
How can i fix this.
please give advice
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论