Golden Master with Example
It can save your days and money too :)

Source Code

The source code the below example is available @ below repositoty

1. Create a unit test and copy the code from GameRunner

    Create a unit test project that references the Trivia project
    Add a unit test
    Copy the GameRunner code over to the unit test
1
public void GameTest()
2
{
3
var aGame = new Game();
4
5
aGame.Add("Chet");
6
aGame.Add("Pat");
7
aGame.Add("Sue");
8
9
var rand = new Random();
10
11
do
12
{
13
aGame.Roll(rand.Next(5) + 1);
14
15
if (rand.Next(9) == 7)
16
{
17
_notAWinner = aGame.WrongAnswer();
18
}
19
else
20
{
21
_notAWinner = aGame.WasCorrectlyAnswered();
22
}
23
} while (_notAWinner);
24
}
Copied!

2. Seed the randomizer or hardcode the inputs

The Golden Master test has to be deterministic. Given input X, we expect output Y every time we run it. Due to randomizer, this is not possible. Hence, we will seed the randomizer so that it would produce the same inputs. Another way could be to replace the randomizer with specific inputs
1
var rand = new Random(345);
Copied!

3. Capture the output

    Run the GameTest unit test in the Test Explorer.
    Click on the GameTest to see the test details.
    Click on Open additional output for this result (Note: it says something different in previous versions of Visual Studio).
    Copy the text.
Note: The output I copied didn’t have a newline at the end, so I had to add one. Watch out for missing newlines when you copy output.
1
private static readonly string GoldenMaster = @"Chet was added
2
They are player number 1
3
...
4
...
Copied!
This is going to be a long text. You could collapse with the quick Visual Studio shortcut " Ctrl+M+M"

4. Capture Console.WriteLine() output into a variable

1
StringBuilder capturedOutput = new StringBuilder();
2
Console.SetOut(new StringWriter(capturedOutput));
Copied!

5. Finally, time to do some test stuff :)

Finally, compare the actual output with the Golden Master. Cpnverting to lines using split and then comparing is actually optional. In case if the output does not match, it would come handy to find which line is not matching.
1
var actualLines = capturedOutput.ToString().Split(new[] {Environment.NewLine}, StringSplitOptions.None);
2
var expectedLines = GoldenMaster.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
3
4
Assert.AreEqual(expectedLines,actualLines);
Copied!
Probably, everything is cool. Sometimes, if everything is cool, it's hard to know if it's really cool or something is wrong
Once in a while, it's a good idea to make an intentional error in an expected output to check if tests are awake.
You made your first Golden Master Test work

6. We could do better

An easy way to do Golden Master testing in C# (also available to Java and many other languages) is to use Approval Tests. It does all the file handling for you, storing and comparing it. Let's see it in action
1
//Add a class atrribute
2
[UseReporter(typeof(DiffReporter))]
3
class GameTests
4
{
5
...
6
}
7
8
//Replace your assesstion with below statement
9
Approvals.Verify(capturedOutput.ToString());
Copied!
The first time the "Approvals.Verify" method is executed, as shown in the above line will generate a text file, in the same folder where the test class is, called: GameTests.TestGame.received.txt . Basically, this is our copy of the working code, if we are happy, we just need to change the .received with .approved to approve the output. Once this is done, every time we run the test, ApprovalTests will compare the output with the approved file.
In our example we are just comparing the string output however, the library is capable of much more. You can explore it.
Now you are ready to rip the trivia code apart. Just make sure you run the tests every time you make a change. :)
Here's the full-fledged refactoring that was done in the game class. Refer to commits history to see how the final classes and tests have evolved
The supporting deck is available @ Surviving Legacy Code

Thank You

Last modified 4mo ago