:= user.CreatePost(c.body) + post, err := user.CreatePost(c.body) + + if err != nil { + t.Fatalf("CreatePost should not return errors: %v", err) + } if got, want := post.UserID, user.ID; got != want { t.Errorf("Created post has userID %d, want %d", got, want) } } === RUN TestCreatePost --- FAIL: TestCreatePost (0.00s) main.go:42: CreatePost should not return errors: body shoud not be empty FAIL
:= range cases { - post, _ := user.CreatePost(c.body) + post, err := user.CreatePost(c.body) + + if err != nil { + t.Fatalf("CreatePost should not return error + } if got, want := post.UserID, user.ID; got != w t.Errorf("Created post has userID %d, want % } } ˞ Ұൠతʹɼςετέʔεͷϧʔϓྑ͘ͳ͍ͱ͞ΕΔ (PͰڐ͞Ε͍ͯΔͷɼAU&SSPSA͕ςετΛࢭΊͳ͍͔Β<ཁग़య>
{ + test: "with oneline body", body: "awesome post", }, { + test: "with empty body", body: "", }, } user := &User{ID: 1} for _, c := range cases { post, err := user.CreatePost(c.body) if err != nil { - t.Fatalf("CreatePost should not return errors: %v", err) + t.Fatalf("%s: CreatePost should not return errors: %v", c.test, err) } if got, want := post.UserID, user.ID; got != want { - t.Errorf("Created post has userID %d, want %d", got, want) + t.Errorf("%s: Created post has userID %d, want %d", c.test, got, want) } } === RUN TestCreatePost --- FAIL: TestCreatePost (0.00s) main.go:42: with empty body: CreatePost should not return errors: body shoud not be empty FAIL
{ post, err := user.CreatePost(c.body) if err != nil { - t.Fatalf("%s: CreatePost should not return + t.Fatalf("CreatePost should not return erro } if got, want := post.UserID, user.ID; got != - t.Errorf("%s: Created post has userID %d, w + t.Errorf("Created post has userID %d, want } + })