使用内部函数进行Golang的HTTP处理程序测试

huangapple go评论81阅读模式
英文:

Golang http handler testing with inner functions

问题

我想为我的HTTP处理程序构建一个测试部分,并且我想使用HTTP处理程序函数的原样,但我想要有单独的函数来模拟处理过程中的数据库部分。我想的是这样的:

package main

import (
	"log"
	"testing"
)

// DB functions
type UserDBFunctions interface {
	InsertUser() string
}

type UserDB struct {
}

func (u UserDB) InsertUser() string {
	return "hello"
}

// http handler functions
type UserHandlerFunctions interface {
	Register() string
}

type UserHandler struct {
	userDb *UserDB
}

func newUserHandler() UserHandler {
	return UserHandler{userDb: new(UserDB)}
}

func (u UserHandler) Register() string {
	str := u.userDb.InsertUser()
	return str + " world"
}

func main() {
	var i UserHandlerFunctions = newUserHandler()
	str :=  i.Register()
	log.Println(str)
}

// test functions
type FakeUserDBFunctions interface {
	InsertUser() string
}

type FakeUserDB struct {
}

func (u FakeUserDB) InsertUser() string {
	return "bye"
}

func newFakeUserHandler() UserHandler {
	return UserHandler{userDb: &FakeUserDB{}}
}

func TestRegisterUser(t *testing.T) {
	t.Run("register user", func(t *testing.T) {
        var i UserHandlerFunctions = newFakeUserHandler()
		str := i.Register()
		log.Println(str)
    })
}

所以第一个接口是真实的数据库函数,然后是调用数据库函数的HTTP处理程序,main()函数下面应该是使用数据库函数的模拟测试。但是这部分代码无法正常工作。我该如何用模拟函数替换newUserHandler中的真实接口。

我真的很需要帮助。😊

英文:

I want to build a testing part for my http handlers, and I want to use the http handler functions how they are, but I want to have separate functions that mock the db part of the handling. What I have in mind is something like:

package main

import (
	"log"
	"testing"
)

// DB functions
type UserDBFunctions interface {
	InsertUser() string
}

type UserDB struct {
}

func (u UserDB) InsertUser() string {
	return "hello"
}

// http handler functions
type UserHandlerFunctions interface {
	Register() string
}

type UserHandler struct {
	userDb *UserDB
}

func newUserHandler() UserHandler {
	return UserHandler{userDb: new(UserDB)}
}

func (u UserHandler) Register() string {
	str := u.userDb.InsertUser()
	return str + " world"
}

func main() {
	var i UserHandlerFunctions = newUserHandler()
	str :=  i.Register()
	log.Println(str)
}

// test functions
type FakeUserDBFunctions interface {
	InsertUser() string
}

type FakeUserDB struct {
}

func (u FakeUserDB) InsertUser() string {
	return "bye"
}

func newFakeUserHandler() UserHandler {
	return UserHandler{userDb: *UserDB(&FakeUserDB{})}
}

func TestRegisterUser(t *testing.T) {
	t.Run("register user", func(t *testing.T) {
        var i UserHandlerFunctions = newFakeUserHandler()
		str := i.Register()
		log.Println(str)
    })
}

So the first interface is for the real db functions, then there is the http handler which calls the db function and below the main() function there should be the tests with a mockup of the db function. But this part doesn't work. How do I replace the real interface in the newUserHandler by a mockup.

I would really appreciate help. 😊

答案1

得分: 2

UseHandler应该使用UserDBFunctions而不是UserDB。

type UserHandler struct {
    userDb UserDBFunctions
}

这是修改后的代码:https://play.golang.org/p/nLiaomKA2NH

英文:

The UseHandler shall use UserDBFunctions instead of UserDB.

type UserHandler struct {
userDb UserDBFunctions
}

Here is the modified one https://play.golang.org/p/nLiaomKA2NH

huangapple
  • 本文由 发表于 2021年9月16日 19:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/69207256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定