Jest创建从存储angular读取的属性模拟

pgx2nnw8  于 2023-09-28  发布在  Jest
关注(0)|答案(1)|浏览(145)

我正在使用Angular、NgRx和Jest。
我正在享受从商店阅读的服务

export class MyFacade {
  configuration$ = this.store.select(selectors.selectConfiguration);
}

我在我的规格文件中使用它,在其中我测试的效果。我创建了一个模拟类,它是这样实现的

export class MyMockFacade {
  configuration$ = of(configObject);
}

我的spec文件看起来像这样:

describe('Config Effects', () => {
  const mockStore = new MockStore();
    ....
  const mockFacade  = new MyMockFacade();

  let actions;
  let effects: ConfigEffects;

  beforeAll(() => {
    Object.defineProperty(window, 'location', {
      configurable: true,
      value: { reload: jest.fn() }
    });
  });
  beforeEach(() => {
    TestBed.configureTestingModule({
     
      providers: [
        ConfigEffects,
        HttpClientModule,
        provideMockActions(() => actions),
        { provide: Store, useValue: mockStore },
        ...
        { provide: MyFacade , useValue: mockFacade  },
      ]
    });
    effects = TestBed.inject(ConfigEffects);
  });
});

我希望能够在测试中更改从配置$返回的值。如何才能实现呢

it('should call ....', () => {
    
    //I would like to be able to assigned new value here!

    
      expect(effects.loadXXX$).toSatisfyOnFlush(() => {
        ...
      });
    });
lf3rwulv

lf3rwulv1#

configuration$设置为BehaviorSubject,以便指定它应该发出的值:

export class MyMockFacade {
  configuration$ = new BehaviorSubject<ConfigObject>(null);
}
it('should call ....', () => {
    mockFacade.configuration$.next(someConfigObject); 

    expect(effects.loadXXX$).toSatisfyOnFlush(() => {
      ...
    });
});

相关问题