带有订阅的商店选择器的角度单元测试组件

发布于 2025-01-22 10:48:54 字数 3842 浏览 0 评论 0原文

我正在使用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

enter image description here

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);

enter image description here

How can i fix this.

please give advice

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文