Issue
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
Solution
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
Answered By – Andrei
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0