[英]Grails 2.5.5 controller unit test cannot cast object error
I've been trying to set up a unit test for a controller. 我一直在尝试为控制器设置单元测试。 However, any attempt to access the
controller
property causes the following error: 但是,任何访问
controller
属性的尝试都会导致以下错误:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class au.org.ala.collectory.ContactController' with class 'java.lang.Class' to class 'au.org.ala.collectory.ContactController'
Here's the code that I've been using: 这是我一直在使用的代码:
package au.org.ala.collectory
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(ContactController)
@Mock([Contact, CollectoryAuthService])
class ContactControllerTests extends Specification {
def contact
def setup() {
contact = new Contact(
title: "Dr",
firstName: "Lemmy",
lastName: "Caution",
phone: "0262465909",
mobile: "0419468551",
email: "lemmy.caution@csiro.au",
notes: "to be treated with exaggerated respect",
publish: true,
userLastModified: 'test')
}
def testList() {
when:
contact.save(flush:true, failOnError: true)
request.contentType = JSON_CONTENT_TYPE
controller.list()
then:
model.contactInstanceList.size() == 1
model.contactInstanceList[0].title == 'Dr'
}
}
I've tried various combinations of code placement and mocking, all to no avail. 我尝试了代码放置和模拟的各种组合,但都无济于事。 Any explanation as to why I would be getting this error would be most appreciated.
对于为什么会出现此错误的任何解释将不胜感激。
The problem is caused by a def name = {
closure in the controller. 该问题是由
def name = {
控制器中的闭包引起的。 This seems to interfere with the AST transformation taking place. 这似乎会干扰正在发生的AST转换。 Changing the closure to a method, ie
def name() {
allows the tests to run. 将闭包更改为方法,即
def name() {
允许测试运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.