2011-05-11 53 views
3

嗨的奇特行爲,我有以下的單元測試,mockDomain

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: [[email protected][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: [[email protected][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: [[email protected][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) 

任何想法?

回答

4

如果你改變產生錯誤的行:

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

 mockDomain(Person, [ person ]) 

是否再工作?我認爲你需要在之前嘲笑域對象你創建一個實例,或者你需要將實例傳遞給mockDomain調用,這樣才能正確設置metaClass

+0

謝謝,100%正確! – Sudarshan 2011-05-12 04:55:15

相關問題