具有ORM交互的单元测试Go函数

时间:2019-02-12 10:58:47

标签: unit-testing go mocking testify

我写了一个函数:

func AllItems(w http.ResponseWriter, r *http.Request) {
    db, err := gorm.Open("sqlite3", "test.db")
    if err != nil {
        panic("failed to connect database")
    }
    defer db.Close()

    var items [] Item
    db.Find(&items)
    fmt.Println("{}", items)

    json.NewEncoder(w).Encode(items)
}

我想对此进行单元测试。理想情况下,单元测试意味着功能的每一行都需要进行测试。我不确定应该如何测试是否打开了数据库连接,然后显示了数据库的所有内容。我应该如何测试此代码?

此功能是简单CRUD应用程序的GET端点。代码为here

1 个答案:

答案 0 :(得分:5)

重构代码,并将其分解为较小的可测试函数,然后将依赖关系传递给这些函数。还为依赖项创建接口,以简化测试。

例如:

type myDatabaseInterface interface {
    Find(interface{}) // this signature should match the real db.Find()
}

func AllItems(w http.ResponseWriter, r *http.Request) {
    db, err := gorm.Open("sqlite3", "test.db")
    if err != nil {
        panic("failed to connect database")
    }
    defer db.Close()
    items := findItems(db)
    json.NewEncoder(w).Encode(items)
}

func find(db myDatabaseInterface) ([]Item) {
    var items []Item
    db.Find(&items)
    return items
}

然后,您可以为依赖项创建模拟并在测试中使用它们:

type mock struct {}

// mock should implement myDatabaseInterface to be able to pass it to the function
func (m *mock) Find(interface{}) {
    // implement the mock to satisfy your test
}

func Test_find(t *testing.T) {
    m := mock{}
    res := find(m)
    // do testing
}

您不必在每次处理请求时都调用Open,而应该在外部打开它并使它可用于您的函数。这样,处理程序变得如此之小,无需真正对其进行测试:

func makeAllItemsHandler(db myDatabaseInterface) func(http.ResponseWriter, *http.Request) {
    return func(http.ResponseWriter, *http.Request) {
        items := findItems(db)
        json.NewEncoder(w).Encode(items)
    }
}

然后,在设置应用程序并将其传递给需要它的功能时,您可以一次创建一个数据库,从而消除了很难测试功能的代码的情况。