javascript - 开 Jest : How to I test a function that calls another function in the body

标签 javascript testing random jestjs

我正在尝试测试 makeRandomComputerMove 函数,但是,我无法使用 Jest 正确模拟从同一模块导出的 getRandomNumber 函数。

如果我直接在测试文件中调用 getRandomNumber 函数,它会按预期工作,但我的印象是,模拟测试文件中的函数应该强制内部 makeRandomComputerMove 函数使用模拟值。

如有任何帮助,我们将不胜感激。

测试.ticTacToe.js

describe('TicTacToe Utils', () => {
    const randomMock = jest.spyOn(Utils, 'getRandomNumber')
        .mockReturnValueOnce(0)
        .mockReturnValueOnce(1)

    const board = [
        [['X'], [], []],
        [[], [], []],
        [[], [], []]
        ]

    it('makeRandomComputerMove', () => {
        const location = Utils.makeRandomComputerMove(board)
        // expect(location.x).toBe(0)
        // expect(location.y).toBe(1)
    })
})

ticTacToe.js

export const getRandomNumber = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

export const makeRandomComputerMove = (board) => {
    const location = {};
    location.x = getRandomNumber(0, board.length - 1);
    location.y = getRandomNumber(0, board.length - 1);
    if (location.x < 3 && location.y < 3) {
        return location

    }
    return makeRandomComputerMove(board);   
};

最佳答案

你将无法 mock getRandomNumber当它被 makeRandomComputerMove 调用时在同一个文件中。

相反,您可以移动 getRandomNumber到一个单独的文件,然后可以对其进行模拟:

getRandomNumber.js

export const getRandomNumber = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

ticTacToe.js

import { getRandomNumber } from './getRandomNumber';

export const makeRandomComputerMove = (board) => {
  const location = {};
  location.x = getRandomNumber(0, board.length - 1);
  location.y = getRandomNumber(0, board.length - 1);
  if (location.x < 3 && location.y < 3) {
    return location
  }
  return makeRandomComputerMove(board)
}

test.ticTacToe.js

import Utils from './'
import { getRandomNumber } from './getRandomNumber'

jest.mock('./getRandomNumber', () => ({
  getRandomNumber: jest.fn()
}))

describe('TicTacToe Utils', () => {
  getRandomNumber.mockReturnValueOnce(0)
  getRandomNumber.mockReturnValueOnce(1)

  const board = [
    [['X'], [], []],
    [[], [], []],
    [[], [], []]
    ]

  it('makeRandomComputerMove', () => {
    const location = Utils.makeRandomComputerMove(board)
    expect(location.x).toBe(0)
    expect(location.y).toBe(1)
  })
})

我希望这会有所帮助。

关于javascript - 开 Jest : How to I test a function that calls another function in the body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60386682/

相关文章:

javascript - HttpTestingController 通过 HttpParams 测试正确的 x-www-form-urlencoded 字段

c++ - 如何在 C 代码上从终端运行 Google 测试?

javascript - jQuery slideUp div 如果已经向下滑动?

javascript - 即时jquery函数

javascript - 使用 React SSR 时如何仅发出一个 API 请求?

ruby-on-rails - 如何启动selenium浏览器并手动关闭它?

java - JSONException : Unparsable JSON string in test

mysql - mysql 中具有局部变量和随机化的 limit union 和 order by 子句的正确语法 : Error(1221)

c - 在 rand() 循环中分配随机浮点值时出现段错误

python - 从固定数量的项目创建随机顺序的组合列表