learn-go-with-tests
learn-go-with-tests copied to clipboard
Bug in RecordWin method on FileSystemPlayerStore
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)
})