我刚刚从 Angular 8 更新到 Angular 9,但遇到了一些我不太明白的问题。我看到一些帖子看起来与我的问题相似,但没有帮助我解决任何问题(可能是因为我而不是其他人给出的解决方案)
所以,问题是当我启动 ng test 时出现此错误:
error TS2339: Property 'payload' does not exist on type 'Action'.
问题出在 spy.calls.allArgs()[1][0].payload.user
上。就像 setUser
和 spy.calls.allArgs
之间的链接不再存在。
login.service.spec.ts
it('should get token', () => {
const store = TestBed.inject(Store);
const spy = spyOn(store, 'dispatch').and.callThrough();
service.getToken('root', '', 'MY_DATABASE').subscribe(() => {
expect(spy).toHaveBeenCalled();
spy.calls.allArgs()[1][0]);
const user: User = spy.calls.allArgs()[1][0].payload.user;
expect(user.database).toBe('MY_DATABASE');
});
});
login.actions.ts
export class SetUser implements Action {
readonly type = LOGIN_ACTION_TYPES.SET_USER;
constructor(public payload: SetUserActionInterface) {}
}
interface SetUserActionInterface {
user: User;
}
我的代码有什么问题?
最佳答案
我不知道这是否是一个好方法,但请尝试:
// we are casting it to be of type SetUser where payload should be there.
const user: User = (spy.calls.allArgs()[1][0] as SetUser).payload.user;
关于javascript - Angular ngrx 属性 'payload' 在类型 'Action' 上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60655500/