0

App.js 看起来像这样,我将如何用玩笑来测试它?

 

    handleClick = e => {
        axios
          .post(
            "https://api.openweathermap.org/data/2.5/weather?q=" +
              this.state.term +
              "&units=metric&appid=" +
              ApiKey
             
          ){...}

我知道我必须创建一个模拟 axios 文件,然后像那样测试它,但它在主 App.js 中使用 setState。我只想开玩笑地测试获取请求/axios 帖子。

4

1 回答 1

0

只需在代码片段下方返回 axios 代码检查的承诺

    // apiActions.js
    const axios = require('axios');
    
    // your api code goes here
    const fetchData = () => {
      return axios
        .get('https://jsonplaceholder.typicode.com/sample_url')
        .then(response => {
          return response.data;
        });
    };

    exports.fetchData = fetchData;

    // testSuit.js
    import fetchData from './apiActions.js';
    // your testing code goes here
    test('testing api call', () => {
    const sampleObj = {name:'',age:39....}
    fetchData().then(resp => {
      expect(resp).toMatchObject(sampleObj)
    })




        
于 2022-01-28T06:36:43.143 回答