Skip to content

Commit

Permalink
test: Check if the callback has been invoked without using exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Aug 25, 2024
1 parent d32d44d commit 033f68a
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions tests/Application.Tests/Maps/LoadTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Constructor_WhenOnLoadingMapIsNull_ShouldThrowArgumentNullException(
static void OnLoadedMap() { }

// Act
Action act = () =>
Action act = () =>
{
var loadTime = new LoadTime(onLoadingMap, OnLoadedMap);
};
Expand Down Expand Up @@ -68,9 +68,11 @@ public void Decrease_WhenIntervalIsNotEqualsToZero_ShouldContinueToDecrease(int
public void Decrease_WhenIntervalIsEqualsToZero_ShouldInvokeOnLoadedMap()
{
// Arrange
bool loadedMap = false;
static void OnLoadingMap() { }
static void OnLoadedMap() => throw new Exception(nameof(OnLoadedMap));
void OnLoadedMap() => loadedMap = true;
var loadTime = new LoadTime(OnLoadingMap, OnLoadedMap);
int expectedInterval = LoadTime.MaxLoadTime;
loadTime.Decrease(); // 5
loadTime.Decrease(); // 4
loadTime.Decrease(); // 3
Expand All @@ -80,32 +82,31 @@ static void OnLoadingMap() { }

// Act
// Invoke to OnLoadedMap
Action act = loadTime.Decrease;
loadTime.Decrease();

// Asserts
act.Should()
.Throw<Exception>()
.WithMessage(nameof(OnLoadedMap));

loadTime.Interval.Should().Be(LoadTime.MaxLoadTime);
loadedMap.Should().BeTrue();
loadTime.Interval.Should().Be(expectedInterval);
loadTime.GameText.Should().BeEmpty();
}

[Test]
public void Decrease_WhenIntervalIsEqualsToMaxLoadTime_ShouldInvokeOnLoadingMap()
{
// Arrange
static void OnLoadingMap() => throw new Exception(nameof(OnLoadingMap));
bool loadingMap = false;
void OnLoadingMap() => loadingMap = true;
static void OnLoadedMap() { }
var loadTime = new LoadTime(OnLoadingMap, OnLoadedMap);
int expectedInterval = 5;

// Act
// Invoke to OnLoadingMap
Action act = loadTime.Decrease;
loadTime.Decrease();

// Assert
act.Should()
.Throw<Exception>()
.WithMessage(nameof(OnLoadingMap));
// Asserts
loadingMap.Should().BeTrue();
loadTime.Interval.Should().Be(expectedInterval);
loadTime.GameText.Should().NotBeEmpty();
}
}

0 comments on commit 033f68a

Please sign in to comment.