In my integration test I do the following:
void testSave() {
def controller = new BookController()
controller.params.title = "Awesome Book"
controller.params.pages = 1000
controller.save()
// i want to check book data here
}
How can I retrieve the Book my controller persisted and check that the title == "Awesome Book" and that pages == 1000? Can I somehow access the bookInstance variable in my controller from the test?
I cannot assume that Book.get(1) will give me the correct Book because there will be bootstrap data in the database. The controller redirects to a completely Book unrelated page so I cannot get the id from the URL. The only thing I can think of is to do something like this:
void testSave() {
def oldIdList = Book.list()*.id
// set up the parameters and call controller.save()
def insertedId = Book.list()*.id - oldIdList
// check Book.get(insertedId) properties
}
but I’m hoping there is a better way…
Assuming you are using Grails 2.0.x you can look up the book by any of its properties using the findBy* methods: