Skip to content
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

Running a function inside a goconvey context #658

Open
ezk84 opened this issue Mar 21, 2022 · 0 comments
Open

Running a function inside a goconvey context #658

ezk84 opened this issue Mar 21, 2022 · 0 comments

Comments

@ezk84
Copy link
Contributor

ezk84 commented Mar 21, 2022

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.

var fakeAction func()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
     // some random stuff with w and r, but then crucially
     fakeAction()
}))

// ...
Convey("something", func() {
      fakeAction = func() {
          // stuff producing err
          So(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:

Convey("something", func(c C) {
    fakeAction = func() {
        // stuff
        c.So(err, ShouldBeNill) // good
    }
    
    http.Get(ts.URL)
})

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.

Then we could have

Convey("something", func(c C) {
    fakeAction = func() {
        // stuff
        c.Run(func() { 
            UtilityThatCallsSo()
        })
    }
      
    http.Get(ts.URL)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant