Nick Vaidyanathan
unread,Oct 24, 2011, 1:52:19 PM10/24/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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!?!?"