zoran119
zoran119

Reputation: 11327

How can I check persisted data inside an integration test

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

Upvotes: 0

Views: 65

Answers (1)

Gregg
Gregg

Reputation: 35904

Assuming you are using Grails 2.0.x you can look up the book by any of its properties using the findBy* methods:

def book = Book.findByTitle("Awesome Book")
assertNotNull book

Upvotes: 2

Related Questions