如何系统测试实时反应多人游戏?

时间:2018-05-17 09:35:34

标签: reactjs testing socket.io integration-testing multiplayer

我正在开发一个使用node和socket.io为后端构建的实时多人棋盘游戏,并为前端做出反应+ redux。这是我第一次做这类项目,即实时和多人游戏。

我不确定如何最好地进行集成/系统测试。我怎么能实际自动化旋转,比如10个前端并让他们一起玩游戏?我应该使用测试框架,如果是这样,哪一个是一个很好的选择,为什么?

1 个答案:

答案 0 :(得分:2)

我发现这个问题与相同的问题。我想出了一种可行的方法,我想您现在也可以,但是要让其他人遇到:

术语说明:可以使用mount()进行整合测试,以与Jest +酶等反应。我基于寻找端到端/验收测试来回答这个问题,实际上,您是在从用户的角度测试产品(这里是在网站上浏览)。

从用户的角度来看,我相信与您使用React无关。

那么,该怎么做呢?有many JS testing options。该资源可以帮助您了解可能要选择的测试包。您需要模拟实际浏览器的东西。

在调查上述资源中列出的一些选项时,我发现:




编辑:我最初建议使用噩梦。但是,在运行多个测试时(出现意外的超时,Electron实例无法正确关闭),我的行为有些奇怪,并研究了其他一些选项。但我会保留信息以供参考:

我之所以选择nightmare是因为它的广告很简单。

下面是一个示例测试,使用Jest和噩梦(以及一些草率的TypeScript)。该站点有一个结束玩家回合的按钮,并且有一个标头,指示该回合是谁。我将模拟单击该按钮并确保标题按预期进行更改。另外请注意,在这些测试期间,您将需要运行开发服务器和前端。

import * as Nightmare from 'nightmare';


let nightmare1: Nightmare;
let nightmare2: Nightmare;

beforeEach(async () => {
  nightmare1 = new Nightmare({ show: true })
  nightmare2 = new Nightmare({ show: true })
  await nightmare1
    .goto('http://127.0.0.1:3000');
  await nightmare2
    .goto('http://127.0.0.1:3000');
});

afterEach(async () => {
  await nightmare1.end();
  await nightmare2.end();
});

it('sockets turn changes via End Turn button', async () => {
  expect.assertions(6);

  // Both display the same player's turn ("Red's Turn")
  const startingTurnIndicator1 = await nightmare1
    .evaluate(() => document.querySelector('h1').innerText);
  const startingTurnIndicator2 = await nightmare2
    .evaluate(() => document.querySelector('h1').innerText);
  expect(startingTurnIndicator1).toBe(startingTurnIndicator2);

  // Both change ("Blue's Turn")
  const oneClickTI1 = await nightmare1
    .click('button')
    .evaluate(() => document.querySelector('h1').innerText)
  const oneClickTI2 = await nightmare2
    .evaluate(() => document.querySelector('h1').innerText);
  expect(oneClickTI1).toBe(oneClickTI2);
      expect(oneClickTI1).not.toBe(startingTurnIndicator1);

  // Both change back ("Red's Turn")
  const twoClickTI2 = await nightmare2
    .click('button')
    .evaluate(() => document.querySelector('h1').innerText)
  const twoClickTI1 = await nightmare1
    .evaluate(() => document.querySelector('h1').innerText);
  expect(twoClickTI1).toBe(twoClickTI2);
  expect(twoClickTI1).toBe(startingTurnIndicator2);
  expect(twoClickTI1).not.toBe(oneClickTI1);
});

我不确定此测试中的实际代码多么好,但是它可以正常工作。