Separated WebSocketService to another service

This commit is contained in:
Martin Berg Alstad
2023-07-18 14:12:53 +02:00
parent ad0d8c7d0a
commit fab3e3d13f
11 changed files with 77 additions and 89 deletions

View File

@ -6,7 +6,6 @@ using Microsoft.Extensions.Logging;
using NSubstitute;
using pacMan.Controllers;
using pacMan.Game;
using pacMan.Interfaces;
using pacMan.Services;
using pacMan.Utils;
@ -16,16 +15,16 @@ public class GameControllerTests
{
private IActionService _actionService = null!;
private GameController _controller = null!;
private IWebSocketService _webSocketService = null!;
private GameService _gameService = null!;
[SetUp]
public void Setup()
{
_webSocketService = Substitute.For<WebSocketService>(Substitute.For<ILogger<WebSocketService>>());
_gameService = Substitute.For<GameService>(Substitute.For<ILogger<GameService>>());
_actionService = Substitute.For<ActionService>(
Substitute.For<ILogger<ActionService>>(), _webSocketService
Substitute.For<ILogger<ActionService>>(), _gameService
);
_controller = new GameController(Substitute.For<ILogger<GameController>>(), _webSocketService, _actionService);
_controller = new GameController(Substitute.For<ILogger<GameController>>(), _gameService, _actionService);
}
[Test]
@ -42,18 +41,14 @@ public class GameControllerTests
var result = runMethod!.Invoke(_controller, new object[] { wssResult, data });
if (result is ArraySegment<byte> resultSegment)
{
Assert.Multiple(() =>
{
Assert.That(resultSegment, Has.Count.EqualTo(data.Length));
Assert.That(Encoding.UTF8.GetString(resultSegment.ToArray()), Is.EqualTo(data.GetString(data.Length)));
});
// TODO not working, works like a normal method call
// _actionService.ReceivedWithAnyArgs().DoAction(default!);
}
// TODO not working, works like a normal method call
// _actionService.ReceivedWithAnyArgs().DoAction(default!);
else
{
Assert.Fail("Result is not an ArraySegment<byte>");
}
}
}

View File

@ -4,7 +4,6 @@ using Microsoft.Extensions.Logging;
using NSubstitute;
using pacMan.Game;
using pacMan.Game.Items;
using pacMan.Interfaces;
using pacMan.Services;
namespace BackendTests.Services;
@ -16,13 +15,13 @@ public class ActionServiceTests
private readonly Player _whitePlayer = (Player)Players.Create("white");
private ActionMessage _blackMessage = null!;
private GameService _gameService = null!;
private ActionMessage _redMessage = null!;
private IActionService _service = null!;
private Queue<DirectionalPosition> _spawns = null!;
private ActionMessage _whiteMessage = null!;
private IWebSocketService _wssSub = null!;
[SetUp]
public void Setup()
@ -43,8 +42,8 @@ public class ActionServiceTests
Action = GameAction.PlayerInfo,
Data = JsonSerializer.Serialize(new { Player = _redPlayer, Spawns = CreateQueue() })
};
_wssSub = Substitute.For<WebSocketService>(Substitute.For<ILogger<WebSocketService>>());
_service = new ActionService(Substitute.For<ILogger<ActionService>>(), _wssSub);
_gameService = Substitute.For<GameService>(Substitute.For<ILogger<GameService>>());
_service = new ActionService(Substitute.For<ILogger<ActionService>>(), _gameService);
}
private static Queue<DirectionalPosition> CreateQueue() =>

View File

@ -3,7 +3,6 @@ using BackendTests.TestUtils;
using Microsoft.Extensions.Logging;
using NSubstitute;
using pacMan.Game;
using pacMan.Interfaces;
using pacMan.Services;
using pacMan.Utils;
@ -14,7 +13,7 @@ public class WebSocketServiceTests
private readonly DirectionalPosition _spawn3By3Up = new()
{ At = new Position { X = 3, Y = 3 }, Direction = Direction.Up };
private IWebSocketService _service = null!;
private GameService _service = null!;
private Queue<DirectionalPosition> _spawns = null!;
@ -22,7 +21,7 @@ public class WebSocketServiceTests
[SetUp]
public void SetUp()
{
_service = new WebSocketService(Substitute.For<ILogger<WebSocketService>>());
_service = new GameService(Substitute.For<ILogger<GameService>>());
_spawns = new Queue<DirectionalPosition>(new[]
{
_spawn3By3Up,
@ -40,7 +39,7 @@ public class WebSocketServiceTests
var segment = "test".ToArraySegment();
using var webSocket = Substitute.For<WebSocket>();
_service.Send(webSocket, segment);
_service.Send(webSocket, segment).Wait();
webSocket.ReceivedWithAnyArgs().SendAsync(default, default, default, default);
}