Details
Description
The mockDomain(Class domainClass, List instances = []) method doesn't act as the documentation stated. While I call save() on the domain object, it doesn't store the new added object to instances. Here is an example reproduced the bug:
// domain class
package my
class Song {
String name
static constraints = { }
}
//unit test
package my
import grails.test.*
class SongTests extends GrailsUnitTestCase {
protected void setUp() { super.setUp() }
protected void tearDown() { super.tearDown() }
void testSomething() {
def songs=[]
mockDomain(Song, songs)
new Song(name:"november rain").save()
assertEquals(songs.size(), 1) // assertion failed. songs.size() remains 0
}
}
Attachments
Issue Links
| This issue is duplicated by: | ||||
| GRAILS-5950 | mockDomain does not work on given instance list |
|
|
|
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
Your test looks something like this:
package my import grails.test.* class SongTests extends GrailsUnitTestCase { void testSomething() { def songs=[] mockDomain(Song, songs) new Song(name:"november rain").save() assertEquals(songs.size(), 1) } }That is testing that calling the save() method adds the instance to the same list that you passed as an argument to mockDomain. The testing framework doesn't do exactly that but does keep track of the newly saved instance. Internally the framework is working with a copy of the list that you passed as an argument. A test like this should pass:
package my import grails.test.* class SongTests extends GrailsUnitTestCase { void testSomething() { def songs=[] mockDomain(Song, songs) assertEquals(0, Song.count()) new Song(name:"november rain").save() assertEquals(1, Song.count()) } }The docs have been updated to clarify this.
See http://github.com/grails/grails/commit/304c98460943f5acf5b977aae9c9dfb65a78722b
package my import grails.test.* class SongTests extends GrailsUnitTestCase { void testSomething() { def songs=[] mockDomain(Song, songs) new Song(name:"november rain").save() assertEquals(songs.size(), 1) } }package my import grails.test.* class SongTests extends GrailsUnitTestCase { void testSomething() { def songs=[] mockDomain(Song, songs) assertEquals(0, Song.count()) new Song(name:"november rain").save() assertEquals(1, Song.count()) } }