Skip to content

第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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ookura-mf
Copy link

@ookura-mf ookura-mf commented Apr 12, 2020

Fixuture使うところまで頑張れませんでした 🙈

@ookura-mf ookura-mf marked this pull request as ready for review April 12, 2020 15:23
Comment on lines +73 to +85
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()

Copy link
Collaborator

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

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)
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

共通したテストデータを使い回す場合にはfixtureだと便利かなーというイメージです。
普通に前後でデータ作って終了時に消すでもいいと思います。

Copy link
Author

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は使いづらいよね・・・

Copy link
Collaborator

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;")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここはdeferで書いてしまったらいいと思います。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants