【问题标题】:Grails Spock test isn't saving the object, I suspect it has something to do with static mapping?Grails Spock 测试没有保存对象,我怀疑它与静态映射有关?
【发布时间】:2014-08-16 17:02:17
【问题描述】:

我已经被这种行为困惑了几个小时了。我正在使用 grails 2.4.2 和 spock 进行测试。我有一个像这样的域对象:

class AccRegStrDb implements Serializable {

    BigInteger accountId
    String keyName
    BigInteger indexNumber
    String value

    static mapping = {
        version false
        keyName column: "`key`"
        indexNumber column: "`index`"
        id generator:'assigned',
                composite: ['accountId', 'keyName', 'indexNumber']
    }
}

请注意,我的班级有 static mapping 行。在我的 spock 测试中,我正在尝试测试将对象保存到数据库中的服务。我有以下代码:

@TestFor(AccountRecordService)
@Mock([AccRegStrDb])
class AccountRecordServiceSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "saveValue() of String saves to AccRegStrDb"() {
        when:
            service.saveValue(2000000, "keyName", "string")

        then:
            1 == AccRegStrDb.count()
        }
}

我的服务如下所示:

@Transactional
class AccountRecordService {
public void saveValue(Long accountId, String keyName, String value) {
        def test = new AccRegStrDb(
                    accountId: accountId,
                    keyName: keyName,
                    indexNumber: BigInteger.ZERO,
                    value: value
            ).save(flush: true, failOnError: true)
    }
}

在测试应用程序上,我的断言失败,因为对象没有保存。没有任何奇怪的错误。当我在课堂上删除 static mapping 时,断言通过了。有什么问题?有什么帮助吗?

:D

【问题讨论】:

    标签: grails


    【解决方案1】:

    通过在我的域类中删除 generator:'assigned' 解决了这个问题。所以,它现在看起来像这样:

    class AccRegStrDb implements Serializable {
    
        BigInteger accountId
        String keyName
        BigInteger indexNumber
        String value
    
        static mapping = {
            version false
            keyName column: "`key`"
            indexNumber column: "`index`"
            id composite: ['accountId', 'keyName', 'indexNumber']
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多