我剛剛開始學習反應單元測試,整個模擬的東西讓我感到困惑,我無法理解。我正在使用 axios 來獲取數據并將其顯示在應用程序組件中:const [ data, setData ] = React.useState(null); useEffect( () => { const url = '/url'; axios.get(url).then((res) => setData(res.data)).catch((err) => { console.log('error happened' + err); }); }, [ data ] ); return ( <div> {data ? <p data-testid="name">{data.name}</p> : <p data-testid="loading">Loading...</p>} </div> );我在 app.test.js 中測試了整個加載和命名:afterEach(cleanup);describe('App', () => { test('Render app', async () => { render(<App />); expect(screen.getByTestId('loading')).toBeInTheDocument(); const name = await waitForElement(() => screen.getByTestId('name')); expect(name).toBeInTheDocument(); expect(screen.queryByTestId('loading')).toBeNull(); });});所有這些測試都成功通過。我想要實現的是如何通過在 app.test.js 中以最簡單的方式模擬它來測試 axios 本身?
如何通過模擬來測試 axios 本身?
森林海
2022-10-13 15:43:30