我已经搜索了高和低,试图让我的笑话测试工作,但在我的搜索一无所获。
我有一个函数(createRoom
),我想对它进行单元测试,我想模拟一个方法(gameFactory.createNewGameWithInitialPlayer()
)调用。
游戏服务.ts
const GameService = (games: {[index: string]: Game}) => {
const playerFactory = PlayerFactory()
const gameFactory = new GameFactory()
const createRoom = ({name, device, socketID}: {name: string, device: string, socketID: string}): RoomResponse => {
const player = playerFactory.createNewPlayer(name, device, socketID)
if (player && player.id) {
const game: Game|undefined = gameFactory.createNewGameWithInitialPlayer(player)
...
}
...
}
游戏工厂.ts
export class GameFactory {
createNewGameWithInitialPlayer = (player: Player): Game|undefined => {
const game = new Game()
game.spectators[player.id as any as number] = player
return game
}
}
游戏服务测试ts
import * as gameFactory from '../Factories/GameFactory'
describe('Testing Game Service', () => {
test('createRoom', () => {
jest.spyOn(gameFactory, 'GameFactory').mockReturnValue({ createNewGameWithInitialPlayer: jest.fn().mockReturnValue(undefined)})
const response: RoomResponse = gameService.createRoom({
name: 'Player 1',
device: DevicesEnum.ios,
socketID: 'some-socket-id'
})
...
}
...
}
在GameService.test.ts
中,我模拟了createNewGameWithInitialPlayer
方法调用的返回值。然而,当我运行测试时,运行的是它的 * 实际 * 实现,而不是我模拟的版本。特别是对于这个测试,我希望createNewGameWithInitialPlayer
方法返回undefined
,但这并没有发生,它似乎在调用实际的方法实现。
2条答案
按热度按时间pbpqsu0x1#
如果你想覆盖 createNewGameWithInitialPlayer 并返回你想要的,你必须在测试中模拟GameFactory类的导入。
如果你想改变你的模拟方法的返回对象,你必须这样做...
5t7ly7z52#
玩笑文件提到
默认情况下,jest.spyOn还会调用spied方法,这是与大多数其他测试库不同的行为,如果想要覆盖原始函数,可以使用jest.spyOn(object,methodName).mockImplementation(()=〉customImplementation)或jest.replaceProperty(object,methodName,jest.fn(()=〉customImplementation));
所以你可以这样做