登录
首页 >  Golang >  Go问答

Golang 中进行依赖注入模拟数据库的单元测试

来源:stackoverflow

时间:2024-02-18 20:18:24 288浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《Golang 中进行依赖注入模拟数据库的单元测试》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在使用 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)
}
  1. 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))
}
  1. 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  
    // but the expected type should be *dbrepo.testdbrepo
    fmt.printf("type of app.db: %t\n", app.db)

    // fails on this line in unit testing
    usercount, err := app.db.getcountbyusername(ctx, username)

    // some other logic
}
  1. setup_test.go
package main

var app application

func TestMain(m *testing.M) {

    app.DB = &dbrepo.TestDBRepo{}

    fmt.Printf("Type of app.DB: %T\n", app.DB) // output type is *dbrepo.TestDBRepo

    os.Exit(m.Run())
}

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

接下来我可以尝试什么?


正确答案


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

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

理论要掌握,实操不能落!以上关于《Golang 中进行依赖注入模拟数据库的单元测试》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>