I keep running into problems with scoping of variables in tests. I eventually get them working, but it’s been trial-and-error.
I either don’t understand the scoping/nesting rules particular to BDD syntax (or closures) or the rules are weird.
Here’s the latest example:
What drives me nuts is that I can debug(variables) and see the thingsToTest key that I want, but I can’t access variables.thingsToTest at the same spot.
The problem is that this code: variables.thingsToTest = [“foo”,“bar”,“baz”];
Will not be available within the describe closures. They are only available at the it() level. Upon execution.
This is due to the fact that we execute the run() method to collect all the test data via describes. Once collected, we then execute the beforeAll and then closures execute. So if you need something before this sequence, just add it at the pseudo constructor level.
Actually, I take it back. It makes the it() titles work (they do change for each loop iteration), but there then is a problem within the it() body: The values, changing with each iteration of the for loop, don’t really change inside the it(). (The value inside the it() always seems to reflect the last value in the loop.)
yes Jamie, that is to be expected. Closures are tricky sometimes to wrap around, no worries.
The issue is that you are statically binding the closures with the for loop. Meaning only the last binding takes effect. If you want to pass in data into the closure you must use the data argument to bind the spec with data at runtime. LIke this: