Closed mattetti closed 11 years ago
Matt,
The reason why this is happening is because Goblin executes everything after all the tests are declared. This means that in your first example, the value of the variable "i" in every test declared inside the loop will be the same.
In order to get your example to work you must do something like this:
for i, exp := range expectations {
thing := things[i]
g.It("each thing must be correct", func() {
t.Log(len(thing.List)) // always prints 0
t.Log(len(exp.List)) // always print 0
})
}
Let me know if that works for you.
Sorry for the lack of feedback,
Quick question, can I do something like this (obviously the following code would fail:
for i, exp := range expectations {
g.It("each thing must be correct", func(thing) {
t.Log(len(thing.List))
t.Log(len(exp.List))
}(things[i]))
}
Basically, I'd like to send arguments to my expectation. This isn't required and I understand that the problem I encountered was a scoping bug in my code and I should have found it myself, but I'm curious to what extent, nested functions can cause more scoping issues.
@mattetti
Sorry for taking too long to respond. I get what you're trying to do, and I don't think we need to change goblin in order to accomplish that.
Let me know if this works for you:
g.Describe("test", func() {
for i := 0; i < 5; i++ {
(func(i int) {
g.It("Is pending "+strconv.Itoa(i), func() {
fmt.Println(i)
})
}(i))
}
})
Marcos.
Oh I see, it delays the running of the "It" function since it's nested the anonymous function. Smart!
On Mon, Dec 2, 2013 at 5:48 PM, Marcos Nils notifications@github.comwrote:
@mattetti https://github.com/mattetti
Sorry for taking too long to respond. I get what you're trying to do, and I don't think we need to change goblin in order to accomplish that.
Let me know if this works for you:
g.Describe("test", func() { for i := 0; i < 5; i++ { (func(i int) { g.It("Is pending "+strconv.Itoa(i), func() { fmt.Println(i) }) }(i)) }})
Marcos.
— Reply to this email directly or view it on GitHubhttps://github.com/franela/goblin/issues/33#issuecomment-29676801 .
I don't have a reproduction for this bug yet, but here is a quick explanation that might make sense:
I have a nested multi describe test, in the last test of the tree, I do a for loop.
I'm very confused why as to why even tho I construct my array with filled up lists, I never get the content inside the scope of the test. However if I move the test function like that:
I'm sure this is something dumb I should know about golang, but that's a very surprising behavior. Any idea what's going on?