我们如何告诉Grails MongoDB单元测试使用db?

时间:2015-01-30 23:50:01

标签: mongodb unit-testing grails

我在MongoDB中设置了一个名为spotlight的数据库。要连接到该数据库,我在DataSource.groovy中使用以下环境设置:

grails {
    mongo {
        host = "localhost"
        port = 27017

    }
}


environments {
    development {   // MongoDB instance running without --auth flag.
        grails {
            mongo {
                databaseName = "spotlight"
            }
        }
    }
    test {
        grails {    // MongoDB instance running without --auth flag.
            mongo {
                databaseName = "spotlight"
            }
        }
    }
}

这是我的单元测试类:

@TestMixin(MongoDbTestMixin)
class BiographySpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "given a bio then find() returns that bio object"() {

        given:
        mongoDomain([Biography])
        Biography bio = new Biography()

        def nameOf1stImage = "firstImage.png"
        def nameOf2ndImage = "secondImage.png"
        def nameInBio = "star"
        def descriptionOfBio = "a description"
        def id = 0;

        bio.firstImage = nameOf1stImage
        bio.secondImage = nameOf2ndImage
        bio.name = nameInBio
        bio.description = descriptionOfBio
        bio.images = [nameOf1stImage,nameOf2ndImage]

        when:
        bio.save(flush:true);
        id = bio.id

        then:
        Biography bioFromDb =  bio.get(id)
        bioFromDb.firstImage == nameOf1stImage
        bioFromDb.secondImage == nameOf2ndImage
        bioFromDb.name == nameInBio
        bioFromDb.description == descriptionOfBio
    }
}

我跑了grails test-app,Grails在测试数据库中创建了一个传记文档,而不是聚光灯数据库。我在DataSource.groovy中配置环境设置的方式有什么问题吗?

0 个答案:

没有答案