Go 中的模拟函数 [英] Mock functions in Go
问题描述
我对依赖感到困惑.我希望能够用模拟函数调用替换一些函数调用.这是我的代码片段:
I'm puzzled with dependencies. I want to be able to replace some function calls with mock ones. Here's a snippet of my code:
func get_page(url string) string {
get_dl_slot(url)
defer free_dl_slot(url)
resp, err := http.Get(url)
if err != nil { return "" }
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil { return "" }
return string(contents)
}
func downloader() {
dl_slots = make(chan bool, DL_SLOT_AMOUNT) // Init the download slot semaphore
content := get_page(BASE_URL)
links_regexp := regexp.MustCompile(LIST_LINK_REGEXP)
matches := links_regexp.FindAllStringSubmatch(content, -1)
for _, match := range matches{
go serie_dl(match[1], match[2])
}
}
我希望能够在不实际通过 http 获取页面的情况下测试 downloader()
- 即通过模拟 get_page
(更容易,因为它只返回页面内容作为字符串)或 http.Get()
.
I'd like to be able to test downloader()
without actually getting a page through http - i.e. by mocking either get_page
(easier since it returns just the page content as a string) or http.Get()
.
我发现了这个话题,这似乎是关于类似的问题.Julian Phillips 提出了他的库 Withmock 作为解决方案,但我无法让它工作.这是我的测试代码的相关部分,老实说,这些代码对我来说主要是货物崇拜代码:
I found this thread which seems to be about a similar problem. Julian Phillips presents his library, Withmock as a solution, but I'm unable to get it to work. Here's the relevant parts of my testing code, which is largely cargo cult code to me, to be honest:
import (
"testing"
"net/http" // mock
"code.google.com/p/gomock"
)
...
func TestDownloader (t *testing.T) {
ctrl := gomock.NewController()
defer ctrl.Finish()
http.MOCK().SetController(ctrl)
http.EXPECT().Get(BASE_URL)
downloader()
// The rest to be written
}
测试输出如下:
错误:无法安装_et/http":退出状态 1 输出:无法加载包:包 _et/http:找到包 http (chunked.go) 和 main(main_mock.go) 在
/var/folders/z9/ql_yn5h550s6shtb9c5sggj40000gn/T/withmock570825607/path/src/_et/http
ERROR: Failed to install '_et/http': exit status 1 output: can't load package: package _et/http: found packages http (chunked.go) and main (main_mock.go) in
/var/folders/z9/ql_yn5h550s6shtb9c5sggj40000gn/T/withmock570825607/path/src/_et/http
Withmock 是我测试问题的解决方案吗?我应该怎么做才能让它工作?
Is the Withmock a solution to my testing problem? What should I do to get it to work?
推荐答案
就我个人而言,我不使用 gomock
(或任何与此相关的模拟框架;没有它,在 Go 中模拟非常容易).我要么将依赖项作为参数传递给 downloader()
函数,要么让 downloader()
成为一个类型的方法,并且该类型可以保存 downloader()
code>get_page 依赖:
Personally, I don't use gomock
(or any mocking framework for that matter; mocking in Go is very easy without it). I would either pass a dependency to the downloader()
function as a parameter, or I would make downloader()
a method on a type, and the type can hold the get_page
dependency:
type PageGetter func(url string) string
func downloader(pageGetterFunc PageGetter) {
// ...
content := pageGetterFunc(BASE_URL)
// ...
}
主要内容:
func get_page(url string) string { /* ... */ }
func main() {
downloader(get_page)
}
测试:
func mock_get_page(url string) string {
// mock your 'get_page()' function here
}
func TestDownloader(t *testing.T) {
downloader(mock_get_page)
}
方法2:使download()
成为Downloader
类型的方法:
如果您不想将依赖项作为参数传递,您还可以将 get_page()
设为类型的成员,并将 download()
设为该类型的方法,然后可以使用 get_page
:
Method2: Make download()
a method of a type Downloader
:
If you don't want to pass the dependency as a parameter, you could also make get_page()
a member of a type, and make download()
a method of that type, which can then use get_page
:
type PageGetter func(url string) string
type Downloader struct {
get_page PageGetter
}
func NewDownloader(pg PageGetter) *Downloader {
return &Downloader{get_page: pg}
}
func (d *Downloader) download() {
//...
content := d.get_page(BASE_URL)
//...
}
主要内容:
func get_page(url string) string { /* ... */ }
func main() {
d := NewDownloader(get_page)
d.download()
}
测试:
func mock_get_page(url string) string {
// mock your 'get_page()' function here
}
func TestDownloader() {
d := NewDownloader(mock_get_page)
d.download()
}
这篇关于Go 中的模拟函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!