Angular Ngrx测试:Store.overrideSelector不起作用
创始人
2024-10-20 09:01:05
0

在Angular Ngrx测试中,有时可能会遇到Store.overrideSelector不起作用的问题。这可能是由于一些常见的错误导致的,下面是一些可能的解决方法:

  1. 确保在测试之前正确导入和配置所需的模块和提供者。例如,在测试文件的顶部,确保导入StoreModule和provideMockStore,并使用mockStoreProvider提供存储。
import { StoreModule } from '@ngrx/store';
import { provideMockStore } from '@ngrx/store/testing';

beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [
      StoreModule.forRoot({}),
    ],
    providers: [
      provideMockStore(),
    ],
  });
});
  1. 确保在测试中正确使用Store.overrideSelector。例如,使用describe和it来定义测试套件和测试用例,并在beforeEach中使用overrideSelector来覆盖选择器。
import { selectCount } from './counter.selectors';

describe('CounterComponent', () => {
  let store: Store;

  beforeEach(() => {
    TestBed.configureTestingModule({
      // ...
    });

    store = TestBed.inject(Store);
    store.overrideSelector(selectCount, 0); // 覆盖选择器
  });

  it('should display the correct count', () => {
    const fixture = TestBed.createComponent(CounterComponent);
    const component = fixture.componentInstance;
    fixture.detectChanges();

    const countElement = fixture.nativeElement.querySelector('.count');
    expect(countElement.textContent).toContain('0'); // 校验选择器的值
  });
});
  1. 确保在测试中正确处理Action的派发和状态更新。例如,通过调用store.dispatch来派发Action,并在测试中订阅store.select以获取状态更新。
import * as CounterActions from './counter.actions';

it('should increment count when increment button is clicked', () => {
  const fixture = TestBed.createComponent(CounterComponent);
  const component = fixture.componentInstance;
  fixture.detectChanges();

  const incrementButton = fixture.nativeElement.querySelector('.increment');
  incrementButton.click();

  store.dispatch(CounterActions.increment()); // 派发Action

  store.select(selectCount).subscribe(count => {
    expect(count).toBe(1); // 校验状态更新
  });
});

通过检查以上几点,你应该能够解决Angular Ngrx测试中Store.overrideSelector不起作用的问题。

相关内容

热门资讯

透视总结!wpk模拟器,wpk... 透视总结!wpk模拟器,wpk辅助软件-竟然是真的有辅助技巧(哔哩哔哩)1、进入游戏-大厅左侧-新手...
透视有挂!wepoker有辅助... 透视有挂!wepoker有辅助功能吗,wepoker智能辅助插件-好像存在有辅助教程(哔哩哔哩)1、...
透视开挂!pokemmo辅助官... 透视开挂!pokemmo辅助官网,cloudpoker怎么开挂-果然有辅助教程(哔哩哔哩)1、下载好...
透视分享!wepoker破解器... 透视分享!wepoker破解器有用吗,wepoker私人局辅助挂-好像是真的有辅助软件(哔哩哔哩)1...
透视辅助!pokemomo辅助... 透视辅助!pokemomo辅助工具,pokernow辅助控制-竟然是真的有辅助软件(哔哩哔哩)1、下...
透视开挂!德普之星透视辅助软件... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
透视普及!wpk辅助是什么,w... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视曝光!wpk辅助器,wpk... 透视曝光!wpk辅助器,wpk软件是真的吗-切实有辅助技巧(哔哩哔哩)透视方法中分为三种模型:靠谱、...
透视普及!wepokerplu... 透视普及!wepokerplus万能挂,wepoker透视脚本免费下载-竟然真的是有辅助软件(哔哩哔...
透视推荐!wepoker辅助器... 透视推荐!wepoker辅助器最新版本更新内容,wepoker科技辅助器-本来是有辅助工具(哔哩哔哩...