mockDomain的特殊行为

时间:2011-05-11 12:23:47

标签: grails groovy spock

您好我有以下单元测试,

 class BookUnitSpec extends UnitSpec  {
def "Person_roleOf() is able to retrive the person role based on the ROLETYPE enum"(){
    setup: "Mock the person object"
    mockDomain(Person); //if I move this line to 3 lines below ... //otherwise the test passes
    def person = new Person()
    def employee = new Employee(id:1)
    def employees = [employee]
           //       mockDomain(Person);    over here I get the below error
    mockDomain(PersonRole,employees);
    mockDomain(Employee,employees);
    when: "Add employee role to the person"
    person.addToPersonRoles(employee);
    person.save()
    then: "Check if the role can be correctly retrieved"
    person!=null
    person.roleOf(ROLETYPES.EMPLOYEE)!=null;
    person.roleOf(ROLETYPES.AUTHOR)==null;
}
}

引发

groovy.lang.MissingMethodException: No signature of method: com.nthdimenzion.domain.base.Person.addToPersonRoles() is applicable for argument types: (com.nthdimenzion.domain.Employee) values: [com.nthdimenzion.domain.Employee@a6cbf7[empId=<null>]]
Possible solutions: addToPersonRoles(java.lang.Object), getPersonRoles()
    at org.nthdimenzion.domain.BookUnitSpec.Person_roleOf() is able to retrive the person role based on the ROLETYPE enum(BookUnitSpec.groovy:21)

groovy.lang.MissingMethodException: No signature of method: com.nthdimenzion.domain.base.Person.addToPersonRoles() is applicable for argument types: (com.nthdimenzion.domain.Employee) values: [com.nthdimenzion.domain.Employee@a6cbf7[empId=<null>]]
Possible solutions: addToPersonRoles(java.lang.Object), getPersonRoles()
    at org.nthdimenzion.domain.BookUnitSpec.Person_roleOf() is able to retrive the person role based on the ROLETYPE enum(BookUnitSpec.groovy:21)

groovy.lang.MissingMethodException: No signature of method: com.nthdimenzion.domain.base.Person.addToPersonRoles() is applicable for argument types: (com.nthdimenzion.domain.Employee) values: [com.nthdimenzion.domain.Employee@a6cbf7[empId=<null>]]
Possible solutions: addToPersonRoles(java.lang.Object), getPersonRoles()
    at org.nthdimenzion.domain.BookUnitSpec.Person_roleOf() is able to retrive the person role based on the ROLETYPE enum(BookUnitSpec.groovy:21)

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

如果更改导致错误的行:

       //       mockDomain(Person);    over here I get the below error

       mockDomain( Person, [ person ] )

它再次起作用吗?我认为您需要在创建实例之前模拟 之前的域对象,或者您需要将实例传递给mockDomain调用,以便可以正确设置metaClass