Open stefanlack opened 10 years ago
Fixed Test:
def "Ensure posts linked to a user can be retrieved"() {
given: "A user with several posts"
User user = new User(loginId: 'joe', password: 'secret')
user.addToPosts(new Post(content: "Second"))
user.addToPosts(new Post(content: "First"))
user.addToPosts(new Post(content: "Third"))
user.save(failOnError: true)
when: "The user is retrieved by their id"
user.discard()
def foundUser = User.get(user.id)
List<String> sortedPostContent = foundUser.posts.collect { it.content
}.sort()
then: "The posts appear on the retrieved user"
sortedPostContent == ['First', 'Second', 'Third']
}
In Listing "3.12 Accessing a User’s posts by walking the object graph" there is a bug in your integration test. When "User.get(user.id)" is called, the user is not read from the database but from the hibernate session instead.
given: user.save(...) user.addToPosts(...) // save is not called here.
when: // the posts are not inserted into the database right now. Add user.discard() // before User.get(user.id), and the test fails.