Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go单元测试-调用数据库事务开始,不是预期的错误_Go_Go Gorm_Testify_Go Sqlmock - Fatal编程技术网

Go单元测试-调用数据库事务开始,不是预期的错误

Go单元测试-调用数据库事务开始,不是预期的错误,go,go-gorm,testify,go-sqlmock,Go,Go Gorm,Testify,Go Sqlmock,我正在尝试使用Data Dog的Go sqlmock和验证在Go中编写模型单元测试 我有以下代码: type Suite struct { suite.Suite DB *gorm.DB mock sqlmock.Sqlmock repository Repository user *models.User } func (s *Suite) SetupSuite() { var ( db *sql.DB

我正在尝试使用Data Dog的
Go sqlmock
验证
在Go中编写模型单元测试

我有以下代码:

type Suite struct {
    suite.Suite
    DB *gorm.DB
    mock sqlmock.Sqlmock

    repository      Repository
    user *models.User
}

func (s *Suite) SetupSuite() {
    var (
        db  *sql.DB
        err error
    )

    db, s.mock, err = sqlmock.New()
    require.NoError(s.T(), err)

    s.DB, err = gorm.Open("mysql", db)
    require.NoError(s.T(), err)

    s.DB.LogMode(true)

    s.repository = CreateRepository(s.DB)
}

func (s *Suite) AfterTest(_, _ string) {
    require.NoError(s.T(), s.mock.ExpectationsWereMet())
}

func TestInit(t *testing.T) {
    suite.Run(t, new(Suite))
}

func (s *Suite) Test_repository_Get() {
    var (
        ID        = 1234
        CountryID = 1
    )

    s.mock.ExpectQuery(regexp.QuoteMeta(
        `SELECT * FROM "User" WHERE (id = $1)`)).
        WithArgs(strconv.Itoa(ID)).
        WillReturnRows(sqlmock.NewRows([]string{"ID", "CountryID"}).
            AddRow(strconv.Itoa(ID), strconv.Itoa(CountryID)))

    res, err := s.repository.Get(ID)

    require.NoError(s.T(), err)
    require.Nil(s.T(), deep.Equal(&models.User{ID: ID, CountryID: CountryID}, res))
}

func (s *Suite) Test_repository_Create() {
    var (
        ID        = 1234
        CountryID = 1
    )

    s.mock.ExpectQuery(regexp.QuoteMeta(
        `INSERT INTO "User" ("ID","CountryID") 
            VALUES ($1,$2) RETURNING "User"."ID"`)).
        WithArgs(ID, CountryID).
        WillReturnRows(
            sqlmock.NewRows([]string{"ID"}).AddRow(strconv.Itoa(ID)))

    err := s.repository.Create(ID, CountryID)

    require.NoError(s.T(), err)
}
但是当我运行
TestInit
时,我得到以下错误:

> call to database transaction Begin, was not expected, next expectation
> is: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
>   - matches sql: 'INSERT INTO "User" \("ID","CountryID"\)             VALUES \(\$1,\$2\) RETURNING "User"\."ID"'
>   - is with arguments:
>     0 - 1234
>     1 - 1
>   - should return rows:
>     row 0 - [1234] [0m
我有几个问题:

  • 这里的问题是什么
  • 是否有办法知道
    SetupSuite
    功能是否正在运行
  • 为什么我得到
    ?我的项目[没有测试文件]
    当我运行
    go test
    时?这是上面代码的文件路径:
    myproject/test/models/User\u test.go

  • 您需要在测试存储库\u创建中添加
    s.mock.ExpectBegin()
    s.mock.ExpectCommit()

    s.mock.ExpectBegin()
    s.mock.ExpectQuery(regexp.QuoteMeta(
            `INSERT INTO "User" ("ID","CountryID") 
                VALUES ($1,$2) RETURNING "User"."ID"`)).
            WithArgs(ID, CountryID).
            WillReturnRows(
                sqlmock.NewRows([]string{"ID"}).AddRow(strconv.Itoa(ID)))
    s.mock.ExpectCommit()
    

    调用数据库事务开始,如果您将gorm版本更新为github.com/jinchu/gorm v1.9.2或最新版本,则错误将得到解决