From 8744c3eb427ad395371e19df394d87e3440ec85d Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sat, 17 Aug 2024 08:34:05 -0500 Subject: [PATCH 1/2] test: Create fake object --- tests/Application.Tests/Flags/FakeCarrier.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/Application.Tests/Flags/FakeCarrier.cs diff --git a/tests/Application.Tests/Flags/FakeCarrier.cs b/tests/Application.Tests/Flags/FakeCarrier.cs new file mode 100644 index 00000000..68280244 --- /dev/null +++ b/tests/Application.Tests/Flags/FakeCarrier.cs @@ -0,0 +1,16 @@ +namespace CTF.Application.Tests.Flags; + +public class FakeCarrier : Player +{ + public override bool SetAttachedObject( + int index, + int modelId, + Bone bone, + Vector3 offset, + Vector3 rotation, + Vector3 scale, + Color materialColor1, + Color materialColor2) => true; + + public override bool RemoveAttachedObject(int index) => true; +} From da84553bc9e9514f84f4ac8afd905fa554ad9eee Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sat, 17 Aug 2024 08:36:06 -0500 Subject: [PATCH 2/2] test: Use FakeCarrier in the tests --- tests/Application.Tests/Flags/FlagTests.cs | 45 ++++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/tests/Application.Tests/Flags/FlagTests.cs b/tests/Application.Tests/Flags/FlagTests.cs index f6e2ca7b..48d883d5 100644 --- a/tests/Application.Tests/Flags/FlagTests.cs +++ b/tests/Application.Tests/Flags/FlagTests.cs @@ -12,10 +12,8 @@ public void IsCaptured_WhenFlagIsCapturedByPlayer_ShouldReturnsTrue() Name = "Red", ColorHex = Color.Red }; - Player player = (Player)Activator.CreateInstance(type: typeof(Player), nonPublic: true); - flag.GetType() - .GetProperty(name: nameof(Flag.FlagCarrier)) - .SetValue(flag, player); + var fakeCarrier = new FakeCarrier(); + flag.SetCarrier(fakeCarrier); // Act bool actual = flag.IsCaptured(); @@ -63,6 +61,25 @@ public void SetCarrier_WhenArgumentIsNull_ShouldThrowArgumentNullException() .WithParameterName(nameof(player)); } + [Test] + public void SetCarrier_WhenArgumentIsValid_ShouldSetPlayerAsCarrier() + { + // Arrange + var flag = new Flag + { + Model = FlagModel.Red, + Name = "Red", + ColorHex = Color.Red + }; + var fakeCarrier = new FakeCarrier(); + + // Act + flag.SetCarrier(fakeCarrier); + + // Assert + flag.FlagCarrier.Should().Be(fakeCarrier); + } + [Test] public void RemoveCarrier_WhenNoPlayerHasCapturedFlag_ShouldNotThrowNullReferenceException() { @@ -80,4 +97,24 @@ public void RemoveCarrier_WhenNoPlayerHasCapturedFlag_ShouldNotThrowNullReferenc // Assert act.Should().NotThrow(); } + + [Test] + public void RemoveCarrier_WhenPlayerHasCapturedFlag_ShouldRemoveFlagOfThatPlayer() + { + // Arrange + var flag = new Flag + { + Model = FlagModel.Red, + Name = "Red", + ColorHex = Color.Red + }; + var fakeCarrier = new FakeCarrier(); + flag.SetCarrier(fakeCarrier); + + // Act + flag.RemoveCarrier(); + + // Assert + flag.FlagCarrier.Should().BeNull(); + } }