1-n relationship and testing

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

1-n relationship and testing

Alessandro F. Pileci
Hi everyone, I use Grails 2.0.0 and Groovy 1.8.5 (JVM 1.6.0_13)
I've developed two simple classes for testing the 1:n relationship
Here they are

package com.book

class Book {

     static hasMany = [sheets:Sheet]

     static constraints = {
     }
}

package com.book

class Sheet {

     static belongsTo = [book:Book]

     static constraints = {
     }
}

I've added the following code to both the unit tests:

void testSomething() {
       def sheet = new Sheet()
       def book = new Book()
       book.addToSheets(sheet)
       assertTrue book.sheets.size() > 0
}

This code runs well in the BookTests.groovy unit testing, but in
SheetTests.groovy unit testing
this code fails with the following exception

No signature of method: com.book.Book.addToSheets() is applicable for
argument types:
(com.book.Sheet) values: [com.book.Sheet : null] Possible solutions:
getSheets()

Any solutions?
Thanks in advance

Alex Pileci

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: 1-n relationship and testing

johnrengelman
You probably need to add a @Mock(Book) to SheetTests.groovy. If you're doing @TestFor(Sheet) on SheetTests.groovy, I think it only mocks the GORM methods on Sheet and not Book. @Mock(Book) should add the methods.

John

On Wed, Feb 22, 2012 at 7:28 AM, Alessandro F. Pileci <[hidden email]> wrote:
Hi everyone, I use Grails 2.0.0 and Groovy 1.8.5 (JVM 1.6.0_13)
I've developed two simple classes for testing the 1:n relationship
Here they are

package com.book

class Book {

   static hasMany = [sheets:Sheet]

   static constraints = {
   }
}

package com.book

class Sheet {

   static belongsTo = [book:Book]

   static constraints = {
   }
}

I've added the following code to both the unit tests:

void testSomething() {
     def sheet = new Sheet()
     def book = new Book()
     book.addToSheets(sheet)
     assertTrue book.sheets.size() > 0
}

This code runs well in the BookTests.groovy unit testing, but in SheetTests.groovy unit testing
this code fails with the following exception

No signature of method: com.book.Book.addToSheets() is applicable for argument types:
(com.book.Sheet) values: [com.book.Sheet : null] Possible solutions: getSheets()

Any solutions?
Thanks in advance

Alex Pileci

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Loading...