You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've recently come across an issue when testing using httptest, and this answer gives a great solution.
To summarise, when a httptest handler runs in the server's own goroutine, that code doesn't run inside a goconvey context, so any calls from that handler to So, Convey, etc end up in a pretty nasty-looking error.
varfakeActionfunc()
ts:=httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r*http.Request) {
// some random stuff with w and r, but then cruciallyfakeAction()
}))
// ...Convey("something", func() {
fakeAction=func() {
// stuff producing errSo(err, ShouldBeNill) // will cause error: So is called without goconvey context
}
http.Get(ts.URL)
})
The solution is to make the enclosing Convey handler pass the convey context explicitly like so:
The problem occurs when one has built up a bunch of testing utilities that call So, that one wants to call from fakeAction. We'd now need to modify all of these to take a goconvey context. This could be made more convenient by having that c C provide something like a Run(f func()) function that allows all calls to goconvey functions inside f to have the right context.
I've recently come across an issue when testing using
httptest
, and this answer gives a great solution.To summarise, when a
httptest
handler runs in the server's own goroutine, that code doesn't run inside a goconvey context, so any calls from that handler toSo
,Convey
, etc end up in a pretty nasty-looking error.The solution is to make the enclosing Convey handler pass the convey context explicitly like so:
The problem occurs when one has built up a bunch of testing utilities that call
So
, that one wants to call fromfakeAction
. We'd now need to modify all of these to take a goconvey context. This could be made more convenient by having thatc C
provide something like aRun(f func())
function that allows all calls to goconvey functions insidef
to have the right context.Then we could have
The text was updated successfully, but these errors were encountered: