-
Notifications
You must be signed in to change notification settings - Fork 25
第7回の課題(大倉) #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
第7回の課題(大倉) #40
Conversation
test_user := domain.User{ | ||
FirstName: "roger", | ||
LastName: "federer", | ||
} | ||
|
||
tx := GetTestTransaction() | ||
id, err := Store(tx, test_user) | ||
if err != nil { | ||
return | ||
} | ||
test_user.ID = id | ||
tx.Commit() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixture使うとymlでテストデータ管理できたりして便利なんですが、おそらくテストケース別にyml書いたり今のところはできないので何が最適解なのかなーといつも思ってます。
https://github.com/mf-https://github.com/go-testfixtures/testfixtures
golang_study/test/api/interfaces/database/user_repository_test.go
Lines 20 to 49 in d2f4472
func TestMain(m *testing.M) { | |
conn, err := sqlx.Open("mysql", "root:rootpassword@tcp(127.0.0.1:3314)/golang_study_test") | |
if err != nil { | |
panic(err.Error()) | |
} | |
err = conn.Ping() | |
if err != nil { | |
panic(err) | |
} | |
db = conn | |
fixtures, err = testfixtures.New( | |
testfixtures.Database(db.DB), | |
testfixtures.Dialect("mysql"), | |
testfixtures.Directory("../../testdata/fixtures"), | |
) | |
if err != nil { | |
panic(err) | |
} | |
os.Exit(m.Run()) | |
} | |
func prepareTestDB() { | |
fmt.Printf("%v", fixtures) | |
if err := fixtures.Load(); err != nil { | |
panic(err) | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
共通したテストデータを使い回す場合にはfixtureだと便利かなーというイメージです。
普通に前後でデータ作って終了時に消すでもいいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほどです
これ、書いてて思ったのは rspec+factory_botって便利すぎるよね
せめてgoにもfactory_bot的なmoduleがあれば・・・
Railsもfixtureは使いづらいよね・・・
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これは作るしかないのか。。
@@ -93,4 +115,5 @@ func TestFirstNameLike(t *testing.T) { | |||
} | |||
}) | |||
} | |||
db.Exec("delete from users;") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここはdeferで書いてしまったらいいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど
Fixuture使うところまで頑張れませんでした 🙈