【问题标题】:peculiar behaviour of mockDomainmockDomain 的特殊行为
【发布时间】:2011-05-11 12:23:46
【问题描述】:

您好,我有以下单元测试,

 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)

有什么想法吗?

【问题讨论】:

    标签: grails groovy spock


    【解决方案1】:

    如果更改导致错误的行:

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

    到

           mockDomain( Person, [ person ] )
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-22
      • 2020-09-26
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多