Started with ActionService tests

This commit is contained in:
Martin Berg Alstad 2023-07-11 20:58:22 +02:00
parent dfd8bbfc10
commit 949c10f9f3
3 changed files with 42 additions and 6 deletions

View File

@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />

View File

@ -1,13 +1,34 @@
using Microsoft.Extensions.Logging;
using NSubstitute;
using pacMan.Game;
using pacMan.Interfaces;
using pacMan.Services;
namespace BackendTests.Services;
public class ActionServiceTests
{
private IActionService _service = null!;
private IWebSocketService _wssMock = null!;
[SetUp]
public void Setup()
{
_wssMock = Substitute.For<WebSocketService>(Substitute.For<ILogger<WebSocketService>>());
_service = new ActionService(Substitute.For<ILogger<ActionService>>(), _wssMock);
}
#region RollDice()
[Test]
public void RollDice_ReturnsListOfIntegers()
{
Assert.Fail();
var dices = _service.RollDice();
Assert.Multiple(() =>
{
Assert.That(dices, Has.Count.EqualTo(2));
Assert.That(dices, Has.All.InRange(1, 6));
});
}
#endregion
@ -17,7 +38,10 @@ public class ActionServiceTests
[Test]
public void PlayerInfo_DataIsNull()
{
Assert.Fail();
var message = new ActionMessage { Action = GameAction.PlayerInfo, Data = "null" };
Assert.Throws<NullReferenceException>(() => _service.PlayerInfo(message));
message.Data = null;
Assert.Throws<NullReferenceException>(() => _service.PlayerInfo(message));
}
[Test]
@ -57,13 +81,13 @@ public class ActionServiceTests
{
Assert.Fail();
}
[Test]
public void Ready_SomeReady()
{
Assert.Fail();
}
[Test]
public void Ready_AllReady()
{

View File

@ -1,4 +1,5 @@
using System.Text.Json;
using Microsoft.CSharp.RuntimeBinder;
using pacMan.Game;
using pacMan.Game.Interfaces;
using pacMan.Game.Items;
@ -51,8 +52,18 @@ public class ActionService : IActionService // TODO tests
public List<IPlayer> PlayerInfo(ActionMessage message)
{
_player = JsonSerializer.Deserialize<Player>(message.Data);
_group = _wsService.AddPlayer(_player);
try
{
_player = JsonSerializer.Deserialize<Player>(message.Data);
_group = _wsService.AddPlayer(_player);
}
catch (RuntimeBinderException e)
{
Console.WriteLine(e);
if (message.Data == null) throw new NullReferenceException();
throw;
}
return _group.Players;
}