假设我在组件中有以下代码,该代码是公共(public)的并在 View 中使用:
removeItemFromCart(item: CartItem): void {
this.store$.dispatch(OrderingStoreActions.removeItemFromCart({ cartItem: item }));
}
在单元测试中,仅测试操作是否已调度就足够了吗?
it("should remove the item from the cart", () => {
const item = <CartItem>{ amount: 1, product: { id: "p" } };
component.removeItemFromCart(item);
expect(store.dispatch).toHaveBeenCalledWith(OrderingStoreActions.removeItemFromCart({ cartItem: item }));
});
或者我应该在模拟存储中测试操作结果是否在功能存储上执行?
最佳答案
您应该对代码是否正确使用给定的 API/库进行单元测试,而不是该 API/库是否有效(这将是集成测试)。因此,您需要确认该操作已使用您打算发送给它的参数进行调度。您在此处显示的测试就足够了。
关于angular - 仅测试 ngrx 操作是否被调用就足够了吗? (单元测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029674/