简体   繁体   中英

Golang gorm mocking

I am using gorm in my projects. Can I mock this database orm for testing without database connection? The problem we have CI tools where I don't have database or database with enough data for testing. 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.

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.

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

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. Can I mock this database orm for testing without database connection? The problem we have CI tools where I don't have database or database with enough data for testing. 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.

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.

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)
    })
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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