0

I stumbled upon something weird as I was writing tests. I have written a lot of table tests and I like the pattern recently I started working on a project that uses Ginkgo, and I wanted to write a table test like I am used to. I spent a few hours not understanding what was going on until I found this: https://github.com/onsi/ginkgo/issues/175#issuecomment-370015433

Which for some reason reassigns the range value in the body of the loop something like this:

for _, test := range tests {
    test := test
    It(test.it, func() {
        // test code reading values from the `test` variable
    })
}

I have done a lot of tests on the following form

for _, test := range tests {
    t.Run(test.name, func(t *testing.T) {
        // test code reading values from the `test` variable
    })
}

And I never had any issues with that. I am interested in what the difference is here.

  • 5
    Because of [this](https://stackoverflow.com/q/45490107/13860) – Jonathan Hall Dec 13 '19 at 10:31
  • We have to guess that it's Flimzy's suggested duplicate, but we can be pretty sure about the guess. If you showed more of the original code—in particular, that there's some goroutines using the values in `test`—we could be sure. – torek Dec 14 '19 at 01:47
  • Even if I showed more of my code you wouldn't be able to tell that that the problem is what Flimzy linked to. Because ginkgo is a library which hides the implementation details. While the post that Flimzy is indeed the cause of this problem. It will be hard to find that solution when you stumble upon this when all you want to do is write a table test in ginkgo. Therefore I wouldn't exactly call this a duplicate. – Benjamin Hammer Nørgaard Dec 18 '19 at 10:26

0 Answers0