Grails

mockDomain(Class domainClass, List instances = []) doesn't store the saved domain object in instances

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.2 final
  • Fix Version/s: 1.2.1
  • Component/s: Testing
  • Labels:
    None
  • Environment:
    grails 1.2, windows xp
  • Testcase included:
    yes

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 }
}

Issue Links

Activity

Hide
Jeff Brown added a comment -

Your test looks something like this:

test/unit/my/SongTests.groovy
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:

test/unit/my/SongTests.groovy
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

Show
Jeff Brown added a comment - Your test looks something like this:
test/unit/my/SongTests.groovy
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:
test/unit/my/SongTests.groovy
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

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: