Grails编译,但Groovy失败

Grails编译,但Groovy失败,grails,groovy,Grails,Groovy,我在Groovy控制台中测试了以下代码,但两者都没有达到预期的效果: 第一个测试: class TekEvent { Date organizer } def tekEvent = new TekEvent(organizer: 'John Doe') assert tekEvent.organizer == 'John Doe' Exception thrown org.codehaus.groovy.runtime.typehandling.GroovyCastEx

我在Groovy控制台中测试了以下代码,但两者都没有达到预期的效果: 第一个测试:

class TekEvent {
  Date organizer 
}

  def tekEvent = new TekEvent(organizer: 'John Doe')
  assert tekEvent.organizer == 'John Doe'

  Exception thrown
  org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'John Doe' with class 'java.lang.String' to class 'java.util.Date'
第二个测试:

class DifferentTekEvent {
  String name 
 }

 def tekEvent = new TekEvent(nameNot: 'John Doe')
 assert tekEvent.nameNot == 'John Doe'
Exception thrown

groovy.lang.MissingPropertyException: No such property: nameNot for class: DifferentTekEvent
等价物在Grails中运行(即,类被实例化并在单元测试中使用),但不会抛出异常。例如:

@TestFor(TekEvent)
class TekEventSpec extends Specification {

void "test"() {

    def tekEvent = new TekEvent(organizer: 'aasdf') //no expceptions thrown here. Why?
    expect:
        tekEvent.organizer == 'aasdf'
}
}
我可以知道为什么会有差异吗?谢谢。

在Grails中,当您构造域类时,将生效

因此不会引发异常,但是如果您看到域实例上的
errors
属性,您将在那里看到一些消息


好的,如果域类在您的示例中没有定义属性(比如'nameNot'),它将忽略该属性。

对,这是为了支持控制器中的
new User(params)
之类的内容,其中
params
映射将包含无法绑定到域类的属性,比如controller和action。