Vuejs2 无法读取属性状态

Vuejs2 无法读取属性状态,vuejs2,jestjs,mocking,state,action,Vuejs2,Jestjs,Mocking,State,Action,我尝试测试此操作: const getGameList = function(context) { if(context.state.user.id){ let request_body = { user_id : context.state.user.id } axios.post(`api/game_list_of_user`,request_body).then(response => { context.commit('Upd

我尝试测试此操作:

const getGameList = function(context) {
  if(context.state.user.id){
    let request_body = {
        user_id : context.state.user.id
    }
    axios.post(`api/game_list_of_user`,request_body).then(response => {
      context.commit('UpdateGameList',response.data);
    }).catch(error => console.log(error));
  }
};
我的操作是获取特定用户的游戏列表。 这项行动有:

  • 作为输入我的用户id
  • 作为我的游戏列表的输出
我的测试:

import actions from '@/store/actions'
import state from '@/store/state'
import store from '@/store'
import axios from 'axios'

jest.mock('axios');

describe('getGameList', () => {
  test('Success: should return the game list of the user and update gameList in the store', () => {
    
    const state = { user: {id: 1} };
    const mockFunction = jest.fn();
    const response = {
      data: [ 
        { id:1, name:"game_name1" },
        { id:2, name:"game_name2" }
      ]
    };

    axios.post.mockResolvedValue(response);

    actions.getGameList({ mockFunction },{ state });

    //expect(mockFunction).toHaveBeenCalledTimes(1);
    //expect(mockFunction).toHaveBeenCalledWith('UpdateGameList',response.data);

  });

  test('Error: an error occurred', () => {
    const errorMessage = 'Error';
    axios.get.mockImplementationOnce(() =>
      Promise.reject(new Error(errorMessage))
    );
  });

});
  • 我声明我的状态(使用我的用户id)
  • 我宣布我预期的答复 来自我的请求(游戏列表=response.data)
  • 我使用jest.fn()模拟该函数。(我应该这样做吗?)
  • 我得到了这个错误:

    我想检查一下:

    • 我的请求已经接到电话了
    • 我请求的响应与我预期的响应匹配
    • 我的变异被称为
    我如何解决这个错误

    我的测试

    jest.mock('axios');
    
    describe('getGameList', () => {
      test('Success: should return the game list of the user and update gameList in the store', () => {
        
        const context = {
          state : { 
            user: {
              id: 1
            } 
          }
        };
        const mockFunction = jest.fn();
        const response = {
          data: [ 
            { id:1, name:"game_name1" },
            { id:2, name:"game_name2" }
          ]
        };
    
        axios.post.mockResolvedValue(response);
    
        actions.getGameList({ mockFunction, context });
    
        expect({ mockFunction, context }).toHaveBeenCalledTimes(1);
        expect(mockFunction).toHaveBeenCalledWith('UpdateGameList',response.data);
    
      });
    
      test('Error: an error occurred', () => {
        const errorMessage = 'Error';
        axios.get.mockImplementationOnce(() =>
          Promise.reject(new Error(errorMessage))
        );
      });
    });
    
    这是我的解决方案:

    import actions from '@/store/actions'
    import mutations from '@/store/mutations'
    import state from '@/store/state'
    import store from '@/store'
    import axios from 'axios'
    
    let url = ''
    let body = {}
    
    jest.mock("axios", () => ({
      post: jest.fn((_url, _body) => { 
        return new Promise((resolve) => {
          url = _url
          body = _body
          resolve(true)
        })
      })
    }))
    
    //https://medium.com/techfides/a-beginner-friendly-guide-to-unit-testing-the-vue-js-application-28fc049d0c78
    //https://www.robinwieruch.de/axios-jest
    //https://lmiller1990.github.io/vue-testing-handbook/vuex-actions.html#testing-actions
    describe('getGameList', () => {
      test('Success: should return the game list of the user and update gameList in the store', async () => {
        const context= {
          state: {
            user: {
              id:1
            }
          },
          commit: jest.fn()
        }
        const response = {
          data: [ 
            { id:1, name:"game_name1" },
            { id:2, name:"game_name2" }
          ]
        };
    
        axios.post.mockResolvedValue(response) //OR axios.post.mockImplementationOnce(() => Promise.resolve(response));
        await actions.getGameList(context)
        expect(axios.post).toHaveBeenCalledWith("api/game_list_of_user",{"user_id":1});
        expect(axios.post).toHaveBeenCalledTimes(1)
        expect(context.commit).toHaveBeenCalledWith("UpdateGameList", response.data)
    
      });
    
      test('Error: an error occurred', () => {
        const errorMessage = 'Error';
        axios.post.mockImplementationOnce(() =>
          Promise.reject(new Error(errorMessage))
        );
      });
    
    });
    

    getGameList的第一个参数(它期望的唯一参数)应该是上下文。你似乎把它作为测试中的第二个参数通过了。那么我应该把“上下文”作为输入,而不是测试中的状态?我试图在测试中声明:const context={state:{user:{id:1}}};但我有同样的错误…你还是把它作为第二个参数传递吗?
    getGameList
    应该如何处理
    {mockFunction}
    ?getGameList应该给我一个对象数组(id和name作为属性)。我只有我的存储的用户id作为参数。我不明白:你还在把它当作第二个论点吗?我只有一个参数。您只定义了一个参数,但传递了两个参数:
    actions.getGameList({mockFunction},{state})