learn-go-with-tests icon indicating copy to clipboard operation
learn-go-with-tests copied to clipboard

Bug in RecordWin method on FileSystemPlayerStore

Open MaT1g3R opened this issue 3 years ago • 0 comments

On the last line of RecordWin, if you write f.database.Encode(league), scores for new players are not correctly persisted, but no tests will catch this error. Adding the below test will catch this bug:

	t.Run("wins for new players are persisted", func(t *testing.T) {
		database, cleanDatabase := createTempFile(t, `[
        {"Name": "Cleo", "Wins": 10},
        {"Name": "Chris", "Wins": 33}]`)
		defer cleanDatabase()

		store, err := NewFileSystemPlayerStore(database)
		AssertNoError(t, err)

		store.RecordWin("Pepper")

		store, err = NewFileSystemPlayerStore(database)
		AssertNoError(t, err)

		got := store.GetPlayerScore("Pepper")
		want := 1
		AssertScoreEquals(t, got, want)
	})

MaT1g3R avatar Apr 12 '21 17:04 MaT1g3R