Changed semantics of Mock in latest Spock 0.6 snapshot

82 views
Skip to first unread message

Nick Vaidyanathan

unread,
Oct 24, 2011, 1:52:19 PM10/24/11
to Spock Framework - User
Learned of this when upgrading from from Grails 2.0 M2 to RC1. Placing
Mock() as a class level variable in UnitSpecs appears to no longer be
valid. Whereas this test used to be green in older Spock, it now gives
errors:


package ...

import grails.plugin.spock.*

class LoginControllerSpec extends ControllerSpec {
def userService = Mock(UserService)
def paneer = new Person(username:"boo")
def "logged in user not made to log in"() {
when: "a user tries to access an action"
mockDomain(Person, [paneer])
controller.session.user = paneer
controller.userService = userService
controller.index()
then: "do not make user log in"
redirectArgs.controller == "blah"
redirectArgs.action == "index"
}

def "bad authenticated user has to re login"() {
given: "a user tries to authenticate with bad data"
controller.params.userId = "paneer"
controller.params.password = "fake"
controller.userService = userService
when: "a user tries to authernticate an action"
controller.auth()
then: "a user has to try again"
controller.flash.message == "Please enter a valid user
name and password"
redirectArgs.action == "index"
}
}



The output says:
LoginControllerSpec
Executed 2 tests with 2 errors .

logged in user not made to log in

Executed in 0.063 seconds.

Mock objects may only be created during the lifetime of a feature
(iteration)

org.spockframework.runtime.InvalidSpecException: Mock objects may only
be created during the lifetime of a feature (iteration)
at spock.lang.Specification.Mock(Specification.java:128)
at spock.lang.Specification.Mock(Specification.java:241)
at com.sig.indication.users.LoginControllerSpec.
$spock_initializer(LoginControllerSpec.groovy:6)

bad authenticated user has to re login

Executed in 0.0 seconds.

Mock objects may only be created during the lifetime of a feature
(iteration)

org.spockframework.runtime.InvalidSpecException: Mock objects may only
be created during the lifetime of a feature (iteration)
at spock.lang.Specification.Mock(Specification.java:128)
at spock.lang.Specification.Mock(Specification.java:241)
at com.sig.indication.users.LoginControllerSpec.
$spock_initializer(LoginControllerSpec.groovy:6)


When I move the userService = Mock(UserService) inside the when/given
blocks, the tests pass.

If this is an intentional change, it makes sense. Though it was nice
to be able to mock collaborators at the class level, I can see the
argument for setting them up before each test.

Just want to make sure people are aware before they go "TEST Y U NO
WORK!?!?"

Peter Niederwieser

unread,
Oct 24, 2011, 3:10:21 PM10/24/11
to spockfr...@googlegroups.com
Nick,

it's certainly not an intended change, and I cannot reproduce it. Can you try again, with latest snapshot and clean build?

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

unread,
Oct 24, 2011, 6:03:36 PM10/24/11
to spockfr...@googlegroups.com
I think we've found the problem. A recent change to 0.6-SNAPSHOT requires every spec to be recompiled, including base specs in other Jars like spock-grails and geb-spock. Can you try with the lastest spock-grails snapshot?

Begin forwarded message:

Nick Vaidyanathan

unread,
Oct 26, 2011, 1:47:59 PM10/26/11
to Spock Framework - User
Verified. Moved Mock back and re-ran without issue, so it must've been
the recompilation step. Thanks!
Reply all
Reply to author
Forward
0 new messages