ruhungry
ruhungry

Reputation: 4696

How to remove object from database in test in Grails

I have got a domain:

MyClass with fields

String a
String b

I got a test:

void testRemoveMyClass() {

    MyClass x = new MyClass()
      x.setId(3)
      x.setA("AAA")
      x.setB("BBB")
      x.save()

    if (!MyClass.exists(3)) {
        fail "Object does not exist"
    }

    x.delete()

    if (MyClass.exists(3)) {
        fail "Object exists"
    }
}

And the second 'fail' fails. How can I delete this object by Id?

Upvotes: 0

Views: 183

Answers (1)

Tom Metz
Tom Metz

Reputation: 919

First of all: do NOT use typed references, use:

def x = new MyClass()

Very good reading about his topic: http://blog.springsource.org/2010/07/28/gorm-gotchas-part-3/

Your object still exists, but it should not be persisted anymore. Try by the end of the test, instead of the second exists():

def y = MyClass.findById(3)
assert y == null

BTW, you can create your domain objects eaisier via map in constructor:

def x = new MyClass(id: 3, a: 'AAA', b: 'BBB')

Upvotes: 1

Related Questions