我正在尝试为域对象创建测试。测试很简单,请参见下文。但是它抛出一个错误

创建名称为'grailsDatastore'的bean时出错

class AccountSpec extends Specification implements DataTest {

    void setupSpec(){
        mockDomain Account
    }

    void "test basic persistence mocking"() {
            setup:
            def account = new Account(name: 'Robert Fripp', username: "robert", password: "robert", email: "[email protected]").save(flush:true)

            expect:
            Account.count() == 1
        }

}

最佳答案

您没有提供足够的信息来确定项目有什么问题。

请参阅https://github.com/jeffbrown/mcroteauaccount上的项目。该测试包含执行您要执行的操作的测试:

https://github.com/jeffbrown/mcroteauaccount/blob/2110545083b3b41dca61eb77f1e4d5dfccc8508a/src/test/groovy/mcroteauaccount/AccountSpec.groovy

package mcroteauaccount

import grails.testing.gorm.DataTest
import spock.lang.Specification

class AccountSpec extends Specification implements DataTest {

    void setupSpec() {
        mockDomain Account
    }

    void "test basic persistence mocking"() {
        setup:
        def account = new Account(name: 'Robert Fripp', username: "robert", password: "robert", email: "[email protected]").save(flush: true)

        expect:
        Account.count() == 1
    }
}

该测试将编译,运行并通过。如果您的问题是如何编写一个保存实例的测试,然后验证该实例是否包含在Account.count()返回的内容中,则上面的代码演示了如何做到这一点。

08-28 22:57