我正在尝试通过以下方式在 Groovy 中模拟 Sql 实例,我正在使用 spock 框架进行测试。但是测试失败,请看下面:
class SQLStatsStorageManagerTest extends Specification {
def mockSql
def setup() {
mockSql = GroovyMock(Sql, global: true)
}
void "SQLStatsStorageManager instantiation succeed"() {
def c
when: "SQLStatsStorageManager is instantiated"
c = new SQLStatsStorageManager("test", [hostname: "localhost", port: 666, database: "db", login: "root", password: "pass"])
then: "there is no error and name is set"
1 * mockSql.newInstance('jdbc:mysql://localhost:666/db', 'root', 'pass', 'com.mysql.jdbc.Driver')
assert c.getName() == "test"
}
}
测试失败,出现以下错误:
Too few invocations for:
1 * mockSql.newInstance('jdbc:mysql://localhost:666/db', 'root', 'pass', 'com.mysql.jdbc.Driver') (0 invocations)
Unmatched invocations (ordered by similarity):
1 * mockSql.newInstance(jdbc:mysql://localhost:666/db, 'root', 'pass', 'com.mysql.jdbc.Driver')
有什么想法吗?
谢谢。
最佳答案
请注意唯一不匹配的参数是数据库链接。
您尝试将其验证为 String
的实例:
'jdbc:mysql://localhost:666/db'
但在不匹配的调用中它是:
jdbc:mysql://localhost:666/db
那么问题来了,它到底是什么?验证类型,问题就解决了。
关于unit-testing - Groovy/模拟 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064339/