Pass current time to model methods by arguments to make them purer and therefore more testable

This commit is contained in:
Vojtěch Káně
2021-03-21 15:30:42 +01:00
parent 22fd8ec17f
commit e5412eba87
3 changed files with 13 additions and 15 deletions

View File

@@ -112,7 +112,7 @@ func TestModel_NextQuestion(t *testing.T) {
m := newTestModelWithData(t)
c := context.Background()
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), c); err != nil {
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613388006, 0), c); err != nil {
t.Fatalf("Unexpected error when switching to next question: %v", err)
}
}
@@ -122,11 +122,11 @@ func TestModel_NextQuestion_noNextQuestion(t *testing.T) {
m := newTestModelWithData(t)
c := context.Background()
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), c); err != nil {
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613388006, 0), c); err != nil {
t.Fatalf("Unexpected error when switching to next question: %v", err)
}
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), c); err == nil {
if err := m.NextQuestion(uuid.MustParse("b3d2f5b2-d5eb-4461-b352-622431a35b12"), time.Unix(1613388008, 0), c); err == nil {
t.Fatalf("Switching to next question from the last one did not fail")
} else if !errors.Is(err, NoNextQuestion) {
t.Fatalf("Unexpected error type after switching to next question from the last one: %v", err)