-
Notifications
You must be signed in to change notification settings - Fork 463
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
where block cannot access or declare variables? #138
Comments
Reported by
|
Reported by |
Reported by |
Reported by |
Reported by |
Reported by |
Reported by
|
It has happened to me multiple times now that I've run into a similar issue and I would very much appreciate if it would at least be possible to access instance variables of the specification from the where block. Consider for example the following test case (which doesn't compile):
Note that I cannot annotate userManager with @shared because then I could not register the listener anymore (which is a mock object). I could of course unroll the |
I was just bitten by this as well, so please use as another datapoint. I was roughly trying to do the following:
|
+1 for this feature! |
2 similar comments
+1 for this feature! |
+1 for this feature! |
@craffael If generally making these fields @tjni Just remove the |
@craffael actually I had a similar problem right now and found an even groovier solution. class SupportedSpec extends Specification {
Consumer<Integer> userDeleteListener = Mock()
def userManager = new UserManager()
def userId1 = userManager.addUser("romeo", "street 1", "+13212342")
def userId2 = userManager.addUser("julia", "street 2", "+43212322")
def setup() {
userManager.addDeleteListener(userDeleteListener)
}
def deleteTest() {
when:
userManager.deleteUser(this."$userId")
then:
1 * userDeleteListener.accept(_)
!userManager.userExists(this."$userId")
where:
userId | _
'userId1' | _
'userId2' | _
}
// more tests that build on userId1, userId2 and userDeleteListener (possibly with where blocks)
} |
Or if you like it better: class SupportedSpec extends Specification {
Consumer<Integer> userDeleteListener = Mock()
def userManager = new UserManager()
def userId1 = userManager.addUser("romeo", "street 1", "+13212342")
def userId2 = userManager.addUser("julia", "street 2", "+43212322")
def setup() {
userManager.addDeleteListener(userDeleteListener)
}
def deleteTest() {
given:
userId = this."$userId"
when:
userManager.deleteUser(userId)
then:
1 * userDeleteListener.accept(_)
!userManager.userExists(userId)
where:
userId | _
'userId1' | _
'userId2' | _
}
// more tests that build on userId1, userId2 and userDeleteListener (possibly with where blocks)
} |
@Vampire Thanks for the tip with dynamic field resolution, that's actually a workable workaround :) |
this really hurts readability. I have a test where I want to test some date ranges. I would have liked to do this: //put this anywhere in the spec
Date longAgo = new Date(System.currentTimeMillis() - 86_400_000_000L)
Date moreRecent = new Date(System.currentTimeMillis() - 86_400_000L)
Date now = new Date()
where:
p1 | p2 | since | until || expect
v1 | v2 | longAgo | now || foo
v2 | v3 | longAgo | moreRecent || bar
v3 | v4 | moreRecent | now || baz But I have to include those long instantiations everywhere, or create methods that return them. |
@chochos you can access |
@Vampire can you expand on your reply to #138 (comment) above? You said "Just remove the where:
sep = "."
path | startsWithPath || result
"" | "" || true
"a${sep}a${sep}a" | "a${sep}b" || false
"a${sep}a" | "a${sep}a" || true
"a${sep}a${sep}a" | "a${sep}a" || true
"a${sep}ab" | "a${sep}a" || false and in 2.1 it gave me I'm trying to something similar in my own code, e.g.: expected = 'Expected'
maps << [
[[VAR: expected]],
[[OTHER: 'Unexpected', VAR: expected]],
[[:], [VAR: expected]],
[[VAR: expected], [:]],
[[VAR: expected], [VAR: 'Unexpected']],
] and I'm also getting that error as well. |
Sorry for the late answer @wisnij. As @leonard84 mentioned, you can declare @leonard84 maybe now that we forbid to access other data variables it might be time to actually reopen this issue and finally allow local variables in the where block? What do you think? |
@Vampire if you want to tackle that, you can give it a try. where:
final sep = "."
path | startsWithPath || result
"" | "" || true
"a${sep}a${sep}a" | "a${sep}b" || false
"a${sep}a" | "a${sep}a" || true The following would be illegal: where:
a | b
1 | 2
final c = a + b
d << [c, c] |
+1 |
Originally reported on Google Code with ID 15
Reported by
jacobsonz
on 2009-03-12 18:07:08The text was updated successfully, but these errors were encountered: