简体   繁体   English

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

[英]How to system-test real-time react multiplayer game?

I am working on a real-time multiplayer board game built using node and socket.io for the backend and react + redux for the frontend. 我正在开发一个实时的多人棋盘游戏,该游戏使用node和socket.io作为后端,并对React + redux作为前端。 It's the first time I'm doing a project of this sort, ie real-time and multiplayer. 这是我第一次进行此类项目,即实时和多人游戏。

I am unsure on how best to go about integration/system testing. 我不确定如何最好地进行集成/系统测试。 How can I actually automate spinning up, say 10 frontends and having them play a game together? 我怎样才能真正实现自动旋转(例如10个前端)并使它们一起玩游戏? Should I use a testing framework for this and if so, which one would be a good choice and why? 我是否应该为此使用测试框架,如果是,那么哪个是一个不错的选择,为什么?

I found this question with the same question. 我发现这个问题与相同的问题。 I have figured out a way for it to work, which I'm guessing you have as well by now, but for someone else to come across: 我想出了一种可行的方法,我想您现在也可以,但是要让其他人遇到:

A clarification on terms: integration testing can be done for react with things like Jest + enzyme, using mount(). 关于术语的澄清:可以使用mount()进行集成测试,以与Jest +酶等反应。 I'm answering this based on looking for end-to-end / acceptance testing, where you're essentially testing your product from the user's standpoint (here, navigating around on a website). 我基于寻找端到端/验收测试来回答这个问题,在这里您实际上是从用户的角度测试产品(在这里浏览网站)。

As this is from the user's perspective, I believe it is irrelevant that you're using React. 因为这是从用户的角度来看的,所以我认为使用React是无关紧要的。

So, how to do this? 那么,该怎么做呢? There are many JS testing options . 许多JS测试选项 That resource can help understand which testing package you might want to select. 该资源可以帮助您了解可能要选择的测试包。 You want something that simulates an actual browser. 您需要模拟实际浏览器的工具。

In investigating some of the options listed in the above resource, I have found that: 在调查上述资源中列出的一些选项时,我发现:




edit: I initially proposed using nightmare. 编辑:我最初建议使用噩梦。 However, I was getting some wonky behavior when running multiple tests (unexpected timeouts, Electron instances not closing properly), and have investigated some other options. 但是,在运行多个测试时(出现意外的超时,Electron实例无法正确关闭),我的行为有些奇怪,并研究了其他一些选项。 But I'll retain the information for reference: 但我会保留信息以供参考:

I selected nightmare because it was advertised as simple. 我选择噩梦是因为它被宣传为简单。

Below is an example test, using Jest and nightmare (and some sloppy TypeScript). 下面是一个使用Jest和噩梦(以及一些草率的TypeScript)的示例测试。 The site has a button to end the player's turn, and there is a header that indicates whose turn it is. 该站点有一个结束玩家回合的按钮,并且有一个标头,指示该回合是谁。 I'll simulate clicking that button and making sure the header changes as expected. 我将模拟单击该按钮并确保标题按预期进行更改。 Also note that you'll need your dev server and frontend running during these tests. 另外请注意,在这些测试期间,您将需要运行开发服务器和前端。

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);
});

I'm not sure how good the actual code in this test is, but it works. 我不确定此测试中的实际代码有多 ,但是它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM