Home > Software design >  Expected spy dispatch to have been called with: object but actual calls were: undefined
Expected spy dispatch to have been called with: object but actual calls were: undefined

Time:10-26

please I have been trying to troubleshoot why I am getting this error with the mockStore.

Expected spy dispatch to have been called with:
  [ Object({ allItem: Object({id: 1, name: milk, price: 5}), type: ‘[Item List] Item List Success' }) ]
but actual calls were:
  [ Object({ allItem: undefined, type: ‘[Item List] Item List Success' }) ]

I have the following configuration

 let store: MockStore<{item: Item}>;
 let initialState = { id:0, name:'', price:0}
 provideMockStore({ initialState })
 store = TestBed.inject(MockStore);

I also tried to remove <{item: Item}> from the MockStore, still the same result.

  it('should dispatch the item’,  () => {
    const storeSpy = spyOn(store, 'dispatch').and.callThrough();

    const item: Item = {
      id: 1,
     name:milk,
     price: 5
    };


  
    component.addCart();
    expect(storeSpy).toHaveBeenCalledWith(
       retrieveItemList({allItem: item })
     );
  });

Below is my action function

export const retrieveItemList = createAction(
    '[Item List] Item List Success',
    props<{allItem: Item}> ()
);

Below is my AppState

export interface AppState {
    item: Item ;
}

I don't have a problem with the implementation, it works perfectly fine except the unit test that is failing. Below is the function for adding to cart

public addCart(): void {
    this.store.dispatch(
         retrieveItemList({ allItem: this.item})
      );
      
     this.router.navigate(['/cart']);
  }

I don't have problem testing the router.navigate or working on selection using overrideSelector. Only this dispatching that is giving me error. Please, pardon me if this is something I should know... a second eye would help me figure out my mistake. I went through the discussion here, I implemented using the setState() but the same error. https://v11.ngrx.io/guide/store/testing

CodePudding user response:

there is an assigment of item to the component field missing

 const item: Item = {
      id: 1,
     name:milk,
     price: 5
    };
component.item = item; // this line is missing
  • Related