【发布时间】:2020-03-31 13:42:32
【问题描述】:
当我使用 Karma 运行测试套件时,我收到了一个似乎来自 AWS Amplify 的错误。
AuthEffects
login
√ should not dispatch any action
√ should call setItem on LocalStorageService
Chrome 78.0.3904 (Windows 10.0.0) ERROR
An error was thrown in afterAll
Uncaught TypeError: Cannot read property 'clientMetadata' of undefined thrown
我认为这个错误是从上次启动的测试中抛出的:AuthEffects
在我的 AuthEffects 中,我必须这样做才能使 AWS 放大工作
import { Auth } from 'aws-amplify';
//...
const promise = Auth.signIn(username, password);
我不明白如何模拟此 API 对 Cognito 的访问。 通常,我通过依赖注入向构造函数提供 Mock 服务,以避免与 API 的真正连接。这里直接导入到组件中。
规格文件:
describe('login', () => {
it('should not dispatch any action', () => {
const actions = new Actions(EMPTY);
const effect = new AuthEffects(
//...
);
const metadata = getEffectsMetadata(effect);
expect(metadata.login).toEqual({ dispatch: false });
});
it('should call setItem on LocalStorageService', () => {
const loginAction = new ActionAuthLogin('test', 'Test1234!');
const source = cold('a', { a: loginAction });
const actions = new Actions(source);
const effect = new AuthEffects(
//...
);
effect.login.subscribe(() => {
expect(localStorageService.setItem).toHaveBeenCalledWith(AUTH_KEY, {
isAuthenticated: true
});
});
});
afterAll(() => {
TestBed.resetTestingModule();
});
});
有没有办法从规范文件中覆盖这个导入?
【问题讨论】:
标签: angular typescript unit-testing mocking aws-amplify