.with closure inside a then: block does not execute

93 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Howard Lewis Ship

ungelesen,
01.05.2012, 16:20:1401.05.12
an spockfr...@googlegroups.com
I've been writing some tests like this:

def "analysis of a simple exception"() {
when:
def ea = analyzer.analyze(t)

then:

ea.exceptionInfos.size() == 1

def ei = ea.exceptionInfos[0]

ei.className == RuntimeException.name
ei.message == message

ei.propertyNames.empty == true
ei.stackTrace.empty == false

where:

message = "Hey! We've Got No Tomatoes"
t = new RuntimeException(message)
}

But I thought I could rewrite it as:

def "analysis of a simple exception"() {
when:
def ea = analyzer.analyze(t)

then:

ea.exceptionInfos.size() == 1

ea.exceptionInfos[0].with {
className == RuntimeException.name
message == message

propertyNames.empty == true
stackTrace.empty == false
}

where:

message = "Hey! We've Got No Tomatoes"
t = new RuntimeException(message)
}

However, I found that I had to directly add an assert to each line
inside the with {} closure.

Is there a magic way that Spock could automatically convert
expressions to inserts in such a block? Alternately, should there be
a method on Specification to provide similar behavior, i.e.

check ea.exceptionInfos[0], {
className == RuntimeException.name
...
}

--
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

Howard Lewis Ship

ungelesen,
01.05.2012, 16:27:4501.05.12
an spockfr...@googlegroups.com
I spoke too soon; looks like Spock really doesn't like what I'm trying to do:


def "analysis of a simple exception"() {
when:
def ea = analyzer.analyze(t)

then:

ea.exceptionInfos.size() == 1

ea.exceptionInfos[0].with {
assert className == RuntimeException.name
assert message == message

assert propertyNames.empty
assert !stackTrace.empty
}

where:

message = "Hey! We've Got No Tomatoes"
t = new RuntimeException(message)
}


Results in a runtime error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Spec expression: 1: unexpected token: assert @ line 1, column 71.
Name == RuntimeException.name assert mes
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:106)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:148)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:141)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:107)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Peter Niederwieser

ungelesen,
01.05.2012, 17:39:4001.05.12
an spockfr...@googlegroups.com
You are running into a known limitation, namely that multi-line conditions don't always work. Also, `with` will return `null` in this case, failing the (outer) condition.

Support for `with` in conditions (without explicit assert) has been requested before. I think that giving a new meaning to Groovy's `with` is too invasive, but I'm thinking about adding `Specification.with` in 0.7.

Cheers,
Peter
> --
> You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
> To post to this group, send email to spockfr...@googlegroups.com.
> To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.
>

Peter Niederwieser

ungelesen,
01.05.2012, 17:51:0301.05.12
an spockfr...@googlegroups.com
By the way, the following should work but isn't pretty:

...

then:
ea.exceptionInfos[0].with {
assert className == RuntimeException.name;
assert message == message;

assert propertyNames.empty;
assert !stackTrace.empty;
true
}

Spock should really throw a more explanatory exception when it hits a problem parsing a multi-line condition. I'll fix this for 0.7.

Cheers,
Peter

On May 1, 2012, at 10:27 PM, Howard Lewis Ship wrote:

Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten