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> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> <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="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" /> <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" /> <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; namespace BackendTests.Services;
public class ActionServiceTests 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() #region RollDice()
[Test] [Test]
public void RollDice_ReturnsListOfIntegers() 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 #endregion
@ -17,7 +38,10 @@ public class ActionServiceTests
[Test] [Test]
public void PlayerInfo_DataIsNull() 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] [Test]
@ -57,13 +81,13 @@ public class ActionServiceTests
{ {
Assert.Fail(); Assert.Fail();
} }
[Test] [Test]
public void Ready_SomeReady() public void Ready_SomeReady()
{ {
Assert.Fail(); Assert.Fail();
} }
[Test] [Test]
public void Ready_AllReady() public void Ready_AllReady()
{ {

View File

@ -1,4 +1,5 @@
using System.Text.Json; using System.Text.Json;
using Microsoft.CSharp.RuntimeBinder;
using pacMan.Game; using pacMan.Game;
using pacMan.Game.Interfaces; using pacMan.Game.Interfaces;
using pacMan.Game.Items; using pacMan.Game.Items;
@ -51,8 +52,18 @@ public class ActionService : IActionService // TODO tests
public List<IPlayer> PlayerInfo(ActionMessage message) public List<IPlayer> PlayerInfo(ActionMessage message)
{ {
_player = JsonSerializer.Deserialize<Player>(message.Data); try
_group = _wsService.AddPlayer(_player); {
_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; return _group.Players;
} }