Interlated
Interlated

Reputation: 5926

Grails Unit test and mocking addTo

I am trying to test the override of equals in a domain object to ensure that 'contains' works. The unit test mechanics are defying me, in spite of documentation and bugs saying that I should be able to mock addTo.

My test is:

@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: "[email protected]")
    member.addToMemberCategories(schoolCat)
    member.addToMemberCategories(membersCat)

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

The error is:

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

The domain object isn't special. MemberCategory implements equals.

This document says @Mock([yyy]) should do the trick: Naleid upgrading to grails 2 testing as does unit testing addto

Upvotes: 1

Views: 529

Answers (1)

Motilal
Motilal

Reputation: 276

I feel you need two changes to make it work

1.Add MemberCategory to @TestFor, because your trying to add members to membercategirt then your code will become like this:

@TestFor(MemberCategory)  
@TestFor(Member)

2.Also call save() after adding member.addToMemberCategories(membersCat), then your code will become some thing like this

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

Hope this helps.

Upvotes: 4

Related Questions