【发布时间】: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