I have a requirement in my grails app to connect to an existing mongodb server. I am using the plugin ':mongodb:3.0.3' to do this. I have a domain class that is basically empty
class Thing {
static mapWith = "mongo"
String id
String name
static constraints = { }
static mapping = {
collection "myThing"
id column: '_id', generator: 'assigned'
version false
}
}
And I am trying to save a new domain object in an integration test -- a precursor to a controller action:
def "create the thing"() {
def thing = new Thing(name: "name", id:"foobar")
when:
def foo = thing.save(flush: true, failOnError:true)
then:
Thing.findByName("name")
}
The test fails because Thing.findByName returns null, however, when I dig into the low level API, I can save an object. The following test passes:
def "create the thing low level"() {
when:
Thing.collection.insert([name:"name"])
then:
Thing.findByName("name")
}
I have looked at the other stackoverflow questions and they seem to deal with:
What am I doing wrong? How can I get GORM saves to work because I would like to use the grails tools available.
NOTE : This is not just a test issue, trying to save something in a controller using GORM also does not work. Calling this action:
def save() {
def thing = new Thing()
thing.name = "foobar"
respond thing.save(failOnError: true, flush: true)
}
Returns { "class": "com.Thing", "id": null, "name": "foobar" }
and a look at the database tells me it is not saved.
You must add this line at the top of your integration test and it should start saving. By default GORM will not persists even in IntegrationTests
static Boolean transactional = false
You test would look something like below,
class TestIntegrationSpec extends IntegrationSpec {
static Boolean transactional = false
def "my test"() {
...
}
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.