Grails单元测试和模拟addTo

时间:2014-02-10 11:48:55

标签: unit-testing grails

我正在尝试在域对象中测试equals的覆盖,以确保'contains'有效。尽管文档和错误说我应该能够模拟addTo,但是单元测试机制正在藐视我。

我的测试是:

@TestFor(Member)
@Mock([Member])
class MemberCategoryTests {

  void testContains() {
    MemberCategory schoolCat = new MemberCategory(name: "SCHOOL")
    MemberCategory membersCat = new MemberCategory(name: "Members")
    Member member = new Member(membershipNumber: "333333",
            surname: "Tester",
            forenames: "Jim",
            preferredEmail: "mmm@yyy.com")
    member.addToMemberCategories(schoolCat)
    member.addToMemberCategories(membersCat)

    MemberCategoryRedback memberCategoryRedback = new MemberCategoryRedback(name: "SCHOOL")
    assert member.memberCategories.contains(memberCategoryRedback)
  }
}

错误是:

No signature of method: au.com.interlated.civiLink.Member.addToMemberCategories() is applicable for argument types: (au.com.interlated.civiLink.MemberCategory) 

域对象并不特殊。 MemberCategory实现equals。

这份文件说@Mock([yyy])应该这样做:Naleid upgrading to grails 2 testingunit testing addto一样

1 个答案:

答案 0 :(得分:4)

我认为您需要进行两项更改才能使其正常工作

1.将MemberCategory添加到@TestFor,因为您尝试将成员添加到membercategirt,那么您的代码就会变成这样:

@TestFor(MemberCategory)  
@TestFor(Member)

2.添加save()后再拨打member.addToMemberCategories(membersCat),那么您的代码就会变成这样的东西

if(!member.save(validate: true,flush:true,failOnError: true)) {    
   member.errors.each {    
      log.debug(it)   
   }   
} 

希望这有帮助。