首页 > 文章列表 > 使用 Golang 依赖注入模拟数据库的单元测试

使用 Golang 依赖注入模拟数据库的单元测试

494 2024-05-02
问题内容

我正在使用 golang 接口创建一个模拟数据库来测试我的处理程序函数。但是,我似乎没有在单元测试环境中正确执行依赖项注入。我创建了一个 setup_test.go 文件来设置我的模拟数据库环境。在下面的代码片段中,我将介绍依赖注入的工作流程。

  1. main.go
package main

type application struct {
    db repository.databaserepo
}

func main() {
    app := application{}

    conn, err := app.connecttodb()
    if err != nil {
        log.fatal("failed to connect to postgresql:", err)
    }

    defer func() {
        conn.close(context.background())
    }()

    app.db = &dbrepo.postgresdbrepo{db: conn}

    // passing app.db to routes function for di
    mux := routes.routes(app.db)
package repository

type databaserepo interface {
    connection() *pgx.conn
    getcountbyusername(ctx context.context, username string) (int, error)
}
  • routes.go
  • package routes
    
    func routes(app repository.databaserepo) http.handler {
        mux := chi.newrouter()
    
        signuph := user.new(app)
        mux.post("/signup", utils.makehttphandler(signuph.signup))
    }
    
  • signup.go

    代码在调用数据库的这一行失败。我已经在 setup_test.go 中的第 4 点设置了我的模拟数据库。这是我收到的错误消息“恐慌:运行时错误:无效的内存地址或零指针取消引用[信号 sigsegv:分段违规...]”。

  • package user
    
    type application struct {
        db repository.databaserepo
    }
    
    // dependency injection of repository (database)
    func new(app repository.databaserepo) *application {
        return &application{db: app}
    }
    
    func (app application) signup(w http.responsewriter, req *http.request) error {
        // some other logic
    
        // when i run `go run .`, this prints out to be *dbrepo.postgresdbrepo
        // when i run `go test -v ./...`, this prints out to be <nil> 
        // but the expected type should be *dbrepo.testdbrepo
        fmt.printf("type of app.db: %tn", app.db)
    
        // fails on this line in unit testing
        usercount, err := app.db.getcountbyusername(ctx, username)
    
        // some other logic
    }
    
  • setup_test.go
  • package main
    
    var app application
    
    func TestMain(m *testing.M) {
    
        app.DB = &dbrepo.TestDBRepo{}
    
        fmt.Printf("Type of app.DB: %Tn", app.DB) // output type is *dbrepo.TestDBRepo
    
        os.Exit(m.Run())
    }
    

    我已经看到许多使用这种方法对类似模拟数据库进行单元测试,但他们通常在同一个包 main 中进行测试,如果您有像我这样的嵌套文件夹和包,则不会出现此问题。

    接下来我可以尝试什么?


    正确答案


    (代表问题作者发布答案,将其移至答案部分)

    解决这个问题的唯一方法似乎是在依赖于这个测试环境(在我的例子中是模拟数据库)的包中创建多个 TestMain,如 https://groups.google.com/g/golang-nuts/c/SxEkZhWl3QA。也许将来这个问题会被添加到 Golang 中,其中 TestMain 可以集成到不同的包中。