-
-
Notifications
You must be signed in to change notification settings - Fork 667
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
Passing reference from Global Setup #18
Comments
I ran into this myself and also thought "does Ginkgo need a beforeAll block"? Then I realized: func TestBooks(t *testing.T) {
RegisterFailHandler(Fail)
//BEFORE ALL
RunSpecs(t, "Books Suite")
//AFTER ALL
} Here's how I share global state: var dbSession *DBSession
func TestBooks(t *testing.T) {
RegisterFailHandler(Fail)
dbSession = DBSession.New(...)
err := dbSession.Connect()
Ω(err).ShouldNot(HaveOccured())
RunSpecs(t, "Books Suite")
dbSession.Disconnect()
} Now your Its have access to the global There's one more trick here. If you want to reset the DB between each test you can do this in your bootstrap file: var dbSession *DBSession
func TestBooks(t *testing.T) {
RegisterFailHandler(Fail)
dbSession = DBSession.New(...)
err := dbSession.Connect()
Ω(err).ShouldNot(HaveOccured())
RunSpecs(t, "Books Suite")
dbSession.Disconnect()
}
var _ = BeforeEach(func() {
err := dbSession.Reset()
Ω(err).ShouldNot(HaveOccured())
}) This is a top-level One last pattern: when I have a lot of infrastructure for a test suite (e.g. an integration test suite that manages multiple external processes) I wrap all that stuff into a separate mothership/provider object and initialize/reset that object in the boostrap but then pull things off of that object throughout my various tests. This keeps the bootstrap file small and centralizes all the infrastructure management to a separate object/package (which can now be shared among tests). |
oops.. didn't mean to close the issue -- i'll let you do that @sporto if you think the approach I outlined above is sufficient. |
Cool this works, thanks. There is a subtle mistake I ran into and I thought this was not working (just writing it here for future reference):
In this case I shadowed the variable dbSession without thinking about it because I needed to get |
Yep... I think I run into this one three times a day ;) Sent from my iPad
|
I would really like to have beforeAll() that will run once before all the |
In my global setup I get a reference to a db session.
Then I my test I will like to use this reference.
How can I do this?
Maybe Ginkgo needs a beforeAll block to do this?
The text was updated successfully, but these errors were encountered: