首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jest和React Native中的异步存储

Jest和React Native中的异步存储
EN

Stack Overflow用户
提问于 2020-12-08 00:15:42
回答 1查看 191关注 0票数 2

我正在学习测试驱动开发,并试图通过遵循https://react-native-async-storage.github.io/async-storage/docs/advanced/jest/的指导来实现异步存储测试。我已经遵循了安装,目前正在尝试在我的第一个测试中模拟异步存储。

我创建了一个简单的测试。

代码语言:javascript
复制
import React from 'react';
import {render, fireEvent} from 'react-native-testing-library';
import UserWelcome from '../UserWelcome';
import AsyncStorage from '@react-native-async-storage/async-storage';

describe('UserWelcome', () => {
  describe('User enters a name and stores in async local storage', () => {
    const firstTimeUser = 'user001';
    let getByTestId;

    beforeEach(() => {
      ({getByTestId} = render(<UserWelcome />));

      fireEvent.changeText(getByTestId('username'), firstTimeUser);
      fireEvent.press(getByTestId('submitUsername'));
    });

    it('checks if Async Storage is used', async () => {
      await asyncOperationOnAsyncStorage();

      expect(AsyncStorage.getItem).toBeCalledWith('currentUser');
    });
  });
});

然后我得到了错误ReferenceError: asyncOperationOnAsyncStorage没有定义,有没有人可以帮我一下,我应该从哪里得到asyncOperationOnAsyncStorage()函数。医生只是说

代码语言:javascript
复制
Each public method available from Async Storage is a mock function, that you can test for certain condition, for example, if .getItem has been called with a specific arguments:
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-08 00:28:30

我想你误解了文档。asyncOperationOnAsyncStorage只是文档中定义的一个示例方法,您必须将asyncOperationOnAsyncStorage替换为您有一些异步存储操作的方法。

因此,从本质上讲,asyncOperationOnAsyncStorage将是包含异步存储逻辑的方法。

代码语言:javascript
复制
asyncOperationOnAsyncStorage = () => { 
   await AsyncStorage.setItem('currentUser', value)
}

您将使用Jest对其进行测试。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65185354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档