简体   繁体   English

Golang gorm 嘲弄

[英]Golang gorm mocking

I am using gorm in my projects.我在我的项目中使用gorm Can I mock this database orm for testing without database connection?我可以在没有数据库连接的情况下模拟这个数据库 orm 进行测试吗? The problem we have CI tools where I don't have database or database with enough data for testing.问题是我们有 CI 工具,我没有数据库或没有足够数据进行测试的数据库。 The other way, I don't want to setup a database for every time I'm testing, because in these cases the CI tool create every time a container just for run the tests.另一种方式,我不想在每次测试时都设置数据库,因为在这些情况下,CI 工具每次都会创建一个容器来运行测试。

Whet is the best way for testing database related methods?测试数据库相关方法的最佳方法是什么? I am using dependency injection in my solutions so it is easy to replace the database with a mocked database.我在我的解决方案中使用依赖注入,因此很容易用模拟数据库替换数据库。 But the gorm have many orm related function.但是gorm有很多与orm相关的功能。

This is a handler for example:这是一个处理程序,例如:

func tokenIntrospectionHandler(db *gorm.DB) http.HandlerFunc {
    return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
        defer req.Body.Close()
        token := req.FormValue("token")
        var resp Response
        json.NewEncoder(w).Encode(resp)
    })
}

for unit tests, this looks pretty good to mock gorm.DB : https://github.com/DATA-DOG/go-sqlmock对于单元测试,模拟gorm.DB看起来不错: https ://github.com/DATA-DOG/go-sqlmock

Here's an example from their site.这是他们网站上的一个例子。 You just creates the mock, then the DB, sets the expectations for the method calls, then run your code under testing and finally check if the expectations are met.您只需创建模拟,然后创建数据库,设置方法调用的预期,然后在测试中运行您的代码,最后检查是否满足预期。


// a successful case
func TestShouldUpdateStats(t *testing.T) {
    db, mock, err := sqlmock.New()
    if err != nil {
        t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    }
    defer db.Close()

    mock.ExpectBegin()
    mock.ExpectExec("UPDATE products").WillReturnResult(sqlmock.NewResult(1, 1))
    mock.ExpectExec("INSERT INTO product_viewers").WithArgs(2, 3).WillReturnResult(sqlmock.NewResult(1, 1))
    mock.ExpectCommit()

    // now we execute our method
    if err = recordStats(db, 2, 3); err != nil {
        t.Errorf("error was not expected while updating stats: %s", err)
    }

    // we make sure that all expectations were met
    if err := mock.ExpectationsWereMet(); err != nil {
        t.Errorf("there were unfulfilled expectations: %s", err)
    }
}

I am using gorm in my projects.我在我的项目中使用gorm Can I mock this database orm for testing without database connection?我可以在没有数据库连接的情况下模拟这个数据库 orm 进行测试吗? The problem we have CI tools where I don't have database or database with enough data for testing.我们有 CI 工具的问题,我没有数据库或数据库有足够的数据进行测试。 The other way, I don't want to setup a database for every time I'm testing, because in these cases the CI tool create every time a container just for run the tests.另一方面,我不想在每次测试时都设置一个数据库,因为在这些情况下,CI 工具每次都会创建一个容器来运行测试。

Whet is the best way for testing database related methods?测试数据库相关方法的最佳方法是什么? I am using dependency injection in my solutions so it is easy to replace the database with a mocked database.我在我的解决方案中使用依赖注入,因此很容易用模拟数据库替换数据库。 But the gorm have many orm related function.但是gorm 有很多与orm 相关的功能。

This is a handler for example:这是一个处理程序,例如:

func tokenIntrospectionHandler(db *gorm.DB) http.HandlerFunc {
    return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
        defer req.Body.Close()
        token := req.FormValue("token")
        var resp Response
        json.NewEncoder(w).Encode(resp)
    })
}

如果您使用的是干净的架构,只需模拟您的存储库,它会好得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM