Go 来自多个测试用例的类似sqlmock期望导致false失败

Go 来自多个测试用例的类似sqlmock期望导致false失败,go,mocking,Go,Mocking,在尝试测试以下函数时: //Transaction Begins a sql transaction. func Transaction(db *sql.DB, txFunc func(*sql.Tx) *errors.ErrorSt) (retErrSt *errors.ErrorSt) { retErrSt = nil tx, retErrSt := beginTrans(db) if retErrSt != nil { return retErrSt

在尝试测试以下函数时:

//Transaction Begins a sql transaction.
func Transaction(db *sql.DB, txFunc func(*sql.Tx) *errors.ErrorSt) (retErrSt *errors.ErrorSt) {
    retErrSt = nil
    tx, retErrSt := beginTrans(db)
    if retErrSt != nil {
        return retErrSt
    }

    defer func() {
        if p := recover(); p != nil {
            tx.Rollback()
            panic(p) // re-throw panic after Rollback
        } else if retErrSt != nil {
            tx.Rollback()
        } else {
            if err := tx.Commit(); err != nil {
                retErrSt = errors.Database().AddDetails(err.Error())
            }
        }
    }()
    retErrSt = txFunc(tx)
    return retErrSt
}
我使用库来模拟数据库操作。到目前为止,这对我很有用,但当我开始堆积测试用例时,它会导致回归

设置

因此,我的测试设置是:

// database mock/stub setup
    db, mock, err := sqlmock.New()
    if !assert.Nilf(t,
        err,
        "Error on trying to start up a stub database connection") {
        t.Fatal()
    }
    defer db.Close()
Error Trace:    dbConnectionsU_test.go:1859
                Error:          Not equal: 
                                expected: &errors.ErrorSt{Code:"", Message:"", Scope:0, Severity:0, Details:errors.LoggerDetails(nil), InnerError:(*errors.ErrorSt)(nil), Stack:""}
                                actual  : &errors.ErrorSt{Code:"db", Message:"", Scope:0, Severity:0, Details:errors.LoggerDetails{"call to database transaction Begin, was not expected, next expectation is: ExpectedCommit => expecting transaction Commit, which should return error: test commit error"}, InnerError:(*errors.ErrorSt)(nil), Stack:"\n\n\tC:/Go/src/runtime/debug/stack.go:24 +0xae\nezsoft/apiserver_sdk/utilities/utilityCore.GetCleanStack(0xc042069470, 0x3, 0x3, 0x3, 0xc0420db8e8)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/utilities/utilityCore/utilityCore.go:56 +0x3b\nezsoft/apiserver_sdk/db.beginTrans(0xc04204d360, 0x0, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:365 +0x100\nezsoft/apiserver_sdk/db.Transaction(0xc04204d360, 0x821690, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:443 +0x3f\nezsoft/apiserver_sdk/db.TestTransaction.func2(0xc0421363c0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU_test.go:1851 +0x75\ntesting.tRunner(0xc0421363c0, 0xc0420e48a0)\n\tC:/Go/src/testing/testing.go:777 +0xd7\ncreated by testing.(*T).Run\n\tC:/Go/src/testing/testing.go:824 +0x2e7\n"}

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,3 +1,3 @@
                                 (*errors.ErrorSt)({
                                - Code: (string) "",
                                + Code: (string) (len=2) "db",
                                  Message: (string) "",
                                @@ -5,5 +5,7 @@
                                  Severity: (errors.Severity) 0,
                                - Details: (errors.LoggerDetails) <nil>,
                                + Details: (errors.LoggerDetails) (len=1) {
                                +  (string) (len=167) "call to database transaction Begin, was not expected, next expectation is: ExpectedCommit => expecting transaction Commit, which should return error: test commit error"
                                + },
                                  InnerError: (*errors.ErrorSt)(<nil>),
                                - Stack: (string) ""
                                + Stack: (string) (len=788) "\n\n\tC:/Go/src/runtime/debug/stack.go:24 +0xae\nezsoft/apiserver_sdk/utilities/utilityCore.GetCleanStack(0xc042069470, 0x3, 0x3, 0x3, 0xc0420db8e8)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/utilities/utilityCore/utilityCore.go:56 +0x3b\nezsoft/apiserver_sdk/db.beginTrans(0xc04204d360, 0x0, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:365 +0x100\nezsoft/apiserver_sdk/db.Transaction(0xc04204d360, 0x821690, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:443 +0x3f\nezsoft/apiserver_sdk/db.TestTransaction.func2(0xc0421363c0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU_test.go:1851 +0x75\ntesting.tRunner(0xc0421363c0, 0xc0420e48a0)\n\tC:/Go/src/testing/testing.go:777 +0xd7\ncreated by testing.(*T).Run\n\tC:/Go/src/testing/testing.go:824 +0x2e7\n"
                                 })
                Test:           TestTransaction/WantErrorFromCallback
测试功能

为了创建这个MVCE,我将它简化为两个测试。当另一个被注释掉时,这两个选项都起作用:

第一次测试

第二次测试

问题

当我确定没有人被评论掉时,我就陷入了混乱。因此,我得到的信息是:

// database mock/stub setup
    db, mock, err := sqlmock.New()
    if !assert.Nilf(t,
        err,
        "Error on trying to start up a stub database connection") {
        t.Fatal()
    }
    defer db.Close()
Error Trace:    dbConnectionsU_test.go:1859
                Error:          Not equal: 
                                expected: &errors.ErrorSt{Code:"", Message:"", Scope:0, Severity:0, Details:errors.LoggerDetails(nil), InnerError:(*errors.ErrorSt)(nil), Stack:""}
                                actual  : &errors.ErrorSt{Code:"db", Message:"", Scope:0, Severity:0, Details:errors.LoggerDetails{"call to database transaction Begin, was not expected, next expectation is: ExpectedCommit => expecting transaction Commit, which should return error: test commit error"}, InnerError:(*errors.ErrorSt)(nil), Stack:"\n\n\tC:/Go/src/runtime/debug/stack.go:24 +0xae\nezsoft/apiserver_sdk/utilities/utilityCore.GetCleanStack(0xc042069470, 0x3, 0x3, 0x3, 0xc0420db8e8)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/utilities/utilityCore/utilityCore.go:56 +0x3b\nezsoft/apiserver_sdk/db.beginTrans(0xc04204d360, 0x0, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:365 +0x100\nezsoft/apiserver_sdk/db.Transaction(0xc04204d360, 0x821690, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:443 +0x3f\nezsoft/apiserver_sdk/db.TestTransaction.func2(0xc0421363c0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU_test.go:1851 +0x75\ntesting.tRunner(0xc0421363c0, 0xc0420e48a0)\n\tC:/Go/src/testing/testing.go:777 +0xd7\ncreated by testing.(*T).Run\n\tC:/Go/src/testing/testing.go:824 +0x2e7\n"}

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,3 +1,3 @@
                                 (*errors.ErrorSt)({
                                - Code: (string) "",
                                + Code: (string) (len=2) "db",
                                  Message: (string) "",
                                @@ -5,5 +5,7 @@
                                  Severity: (errors.Severity) 0,
                                - Details: (errors.LoggerDetails) <nil>,
                                + Details: (errors.LoggerDetails) (len=1) {
                                +  (string) (len=167) "call to database transaction Begin, was not expected, next expectation is: ExpectedCommit => expecting transaction Commit, which should return error: test commit error"
                                + },
                                  InnerError: (*errors.ErrorSt)(<nil>),
                                - Stack: (string) ""
                                + Stack: (string) (len=788) "\n\n\tC:/Go/src/runtime/debug/stack.go:24 +0xae\nezsoft/apiserver_sdk/utilities/utilityCore.GetCleanStack(0xc042069470, 0x3, 0x3, 0x3, 0xc0420db8e8)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/utilities/utilityCore/utilityCore.go:56 +0x3b\nezsoft/apiserver_sdk/db.beginTrans(0xc04204d360, 0x0, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:365 +0x100\nezsoft/apiserver_sdk/db.Transaction(0xc04204d360, 0x821690, 0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU.go:443 +0x3f\nezsoft/apiserver_sdk/db.TestTransaction.func2(0xc0421363c0)\n\tD:/dev2017/GO/src/ezsoft/apiserver_sdk/db/dbConnectionsU_test.go:1851 +0x75\ntesting.tRunner(0xc0421363c0, 0xc0420e48a0)\n\tC:/Go/src/testing/testing.go:777 +0xd7\ncreated by testing.(*T).Run\n\tC:/Go/src/testing/testing.go:824 +0x2e7\n"
                                 })
                Test:           TestTransaction/WantErrorFromCallback
Error Trace:dbConnectionsU\u test.go:1859
错误:不等于:
应为:&errors.ErrorSt{Code:,消息:,作用域:0,严重性:0,详细信息:errors.LoggerDetails(nil),InnerError:(*errors.ErrorSt)(nil),堆栈:}
实际:&errors.ErrorSt{Code:“db”,消息:,作用域:0,严重性:0,详细信息:错误。对数据库事务开始的LoggerDetails{”调用不是预期的,下一个预期是:ExpectedCommit=>预期事务提交,它应该返回错误:test Commit error“},InnerError:(*errors.ErrorSt)(nil),堆栈:“\n\n\tC:/Go/src/runtime/debug/stack.Go:24+0xae\nezsoft/apiserver\u sdk/utilityCore.GetCleanStack(0xc042069470、0x3、0x3、0xc0420db8e8)\n\tD:/dev2017/Go/src/ezsoft/apiserver\u sdk/utilityCore/utilityCore.Go:56+0x3b\nezsoft/apiserver\u sdk/db.beginters(0xc04204d360、0x0、0x0、0x0、0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver\u sdk/db/dbConnectionsU.GO:365+0x100\nezsoft/apiserver\u sdk/db.Transaction(0xc04204d360,0x821690,0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver\u sdk/db/dbConnectionsU.GO:443+0x3f\nezsoft/apiserver\u sdk/db.TestTransaction.func2(0xc0421363c0)\n\tD:/dev2017/GO/src/ezsoft/apiserver\u sdk/db/dbConnectionsU\u test.GO:1851+0x75\ntesting.tRunner(0xc0421363c0,0xc0420e48a0)\n\tC:/GO/src/testing/testing.GO:777+0xd7\n由测试创建。(*T)。运行\n\tC:/GO/src/testing/testing/testing.GO:824+0x2e7\n“
差异:
---期望
+++实际的
@@ -1,3 +1,3 @@
(*errors.ErrorSt)({
-代码:(字符串)“,
+代码:(字符串)(len=2)“db”,
消息:(字符串)“,
@@ -5,5 +5,7 @@
严重性:(错误。严重性)0,
-详细信息:(错误。日志详细信息),
+详细信息:(errors.LoggerDetails)(len=1){
+(string)(len=167)“对数据库事务开始的调用不是预期的,下一个预期是:ExpectedCommit=>expecting transaction Commit,它应该返回错误:test Commit error”
+ },
InnerError:(*errors.ErrorSt)(,
-堆栈:(字符串)”
+Stack:(string)(len=788)“\n\n\tC:/Go/src/runtime/debug/Stack.Go:24+0xae\nezsoft/apiserver\u sdk/utilityCore/utilityCore.GetCleanStack(0xc042069470,0x3,0x3,0x3,0xc0420db8e8)\n\tD:/dev2017/Go/src/ezsoft/apiserver\u sdk/utilities/utilityCore/utilityCore/utilityCore/utilityCore.Go:56+0x3b\nezsoft/apiserver\u sdk/db.beginterns(0xc04204d360,0x0,0x0,0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver\u sdk/db/dbConnectionsU.GO:365+0x100\nezsoft/apiserver\u sdk/db.Transaction(0xc04204d360,0x821690,0x0)\n\tD:/dev2017/GO/src/ezsoft/apiserver\u sdk/db/dbConnectionsU.GO:443+0x3f\nezsoft/apiserver\u sdk/db.TestTransaction.func2(0xc0421363c0)\n\tD:/dev2017/GO/src/ezsoft/apiserver\u sdk/db/dbConnectionsU\u test.GO:1851+0x75\ntesting.tRunner(0xc0421363c0,0xc0420e48a0)\n\tC:/GO/src/testing/testing.GO:777+0xd7\n由测试创建。(*T)。运行\n\tC:/GO/src/testing/testing/testing.GO:824+0x2e7\n”
})
测试:TestTransaction/WantErrorFromCallback
这就像从未预料到的对
Begin
的调用一样,但在这两种情况下,第一行代码实际上都是
mock.ExpectBegin()

我怀疑它可能必须处理
延迟
红色代码,无论是在被测函数中还是在我的测试代码中,但我不知道如何控制它。此外,我不确定我的怀疑是否正确。

哇,我觉得很傻

问题在于同步,而且显然是陈旧的模拟状态

解决方案

我没有采用标准方法,而是将测试本身重新分解为套件方法,这些方法在
defer
红色函数中断言
ExpectationsWereMet()
。预期(
ExpectCommit
ExpectRollback
,…)是首先发生的事情

真不敢相信我只是为了这么简单的事才提出这个问题