Press "Enter" to skip to content

Lesson 17.4: Automated test for saved game reader

The last thing we’ll do with the new saved game logic is create an automated test that ensures we don’t break that function when we make more changes.

I’ll also talk about opening up this project to more contributors.

Step 1: Copy saved game to \TestEngine\TestFiles\SavedGames\Game_0_1_000.soscsrpg.

Add the new \TestFiles\SavedGames directory to the TestEngine project. Copy the Game_0_1_000.soscsrpg file from this page. Set its “Copy to Output Directory” to “Copy Always”.


  "Version": "0.1.000",
  "CurrentPlayer": {
    "CharacterClass": "Fighter",
    "ExperiencePoints": 20,
    "Quests": [
        "PlayerQuest": {
          "ID": 1
        "IsCompleted": false
    "Recipes": [
        "ID": 1
    "Name": "Scott",
    "Dexterity": 18,
    "CurrentHitPoints": 8,
    "MaximumHitPoints": 10,
    "Gold": 1000000,
    "Level": 1,
    "Inventory": {
      "Items": [
          "ItemTypeID": 1001
          "ItemTypeID": 2001
          "ItemTypeID": 3001
          "ItemTypeID": 3002
          "ItemTypeID": 3003
    "CurrentWeapon": null,
    "CurrentConsumable": null
  "CurrentLocation": {
    "XCoordinate": 0,
    "YCoordinate": 1

Step 2: Create new \TestEngine\Services\TestSaveGameService.cs unit test class

Add the “using” directives for Engine.Services and Engine.ViewModels and create a test function for version 0.1.000.

On line 14, we read the saved game from our test file and store it to a local GameSession object.

Then, we have assertions from line 19 through 48 to check that all the values were correctly loaded.

using System.Linq;
using Engine.Services;
using Engine.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestEngine.Services
    public class TestSaveGameService
        public void Test_Restore_0_1_000()
            GameSession gameSession =
            // Game session data
            Assert.AreEqual("0.1.000", gameSession.Version);
            Assert.AreEqual(0, gameSession.CurrentLocation.XCoordinate);
            Assert.AreEqual(1, gameSession.CurrentLocation.YCoordinate);
            // Player data
            Assert.AreEqual("Fighter", gameSession.CurrentPlayer.CharacterClass);
            Assert.AreEqual("Scott", gameSession.CurrentPlayer.Name);
            Assert.AreEqual(18, gameSession.CurrentPlayer.Dexterity);
            Assert.AreEqual(8, gameSession.CurrentPlayer.CurrentHitPoints);
            Assert.AreEqual(10, gameSession.CurrentPlayer.MaximumHitPoints);
            Assert.AreEqual(20, gameSession.CurrentPlayer.ExperiencePoints);
            Assert.AreEqual(1, gameSession.CurrentPlayer.Level);
            Assert.AreEqual(1000000, gameSession.CurrentPlayer.Gold);
            // Player quest data
            Assert.AreEqual(1, gameSession.CurrentPlayer.Quests.Count);
            Assert.AreEqual(1, gameSession.CurrentPlayer.Quests[0].PlayerQuest.ID);
            // Player recipe data
            Assert.AreEqual(1, gameSession.CurrentPlayer.Recipes.Count);
            Assert.AreEqual(1, gameSession.CurrentPlayer.Recipes[0].ID);
            // Player inventory data
            Assert.AreEqual(5, gameSession.CurrentPlayer.Inventory.Items.Count);
            Assert.AreEqual(1, gameSession.CurrentPlayer.Inventory.Items.Count(i => i.ItemTypeID.Equals(1001)));
            Assert.AreEqual(1, gameSession.CurrentPlayer.Inventory.Items.Count(i => i.ItemTypeID.Equals(2001)));
            Assert.AreEqual(1, gameSession.CurrentPlayer.Inventory.Items.Count(i => i.ItemTypeID.Equals(3001)));
            Assert.AreEqual(1, gameSession.CurrentPlayer.Inventory.Items.Count(i => i.ItemTypeID.Equals(3002)));
            Assert.AreEqual(1, gameSession.CurrentPlayer.Inventory.Items.Count(i => i.ItemTypeID.Equals(3003)));

Step 3: Run the tests

Since we added a new unit test, run all the unit tests and make sure they all pass.

Opening up the project to more contributors

Several people have built their own version of this game, with new features, and asked if they could contribute to the main project.

I haven’t run an open source project with other contributors, but I think it’s a great idea to get more people involved. Personally, I need to spend time on my consulting business, and this should let us get faster improvements to the project.

To help coordinate work, I added a wiki page for the project and am now adding bugs and feature requests through the issues page.

If you want to contribute changes and bug fixes, and you have a GitHub account, please leave a message below.

For now, I’ll be the only approver for pull requests, but we’ll adapt as we go along.

Please let me know if you have any suggestions or questions.

NEXT LESSON: Lesson 99.1: Fix event subscriptions

PREVIOUS LESSON: Lesson 17.3: Add a menu to save and load the game state


  1. Steven
    Steven 2021-11-26

    When I run the TestSaveGameService.cs unit test
    i get this error:

    Source: TestSavedGameService.cs line 14
    Duration: 548 ms

    Assert.AreEqual failed. Expected:. Actual:.

    Stack Trace:

    do we need a saved game first?

      SOSCSRPG 2021-11-26

      Hi Steven,

      Did you create the Game_0_1_000.soscsrpg file from the page (or copy it from GitHub)? Also, make sure to right-click on Game_0_1_000.soscsrpg in Solution Explorer and set its “Copy to Output Directory” value to “Copy Always”.

      Let me know if those do not fix the problem.

Leave a Reply

Your email address will not be published. Required fields are marked *