登录
首页 >  Golang >  Go问答

检查函数是否会调用另一个函数

来源:stackoverflow

时间:2024-04-29 15:18:37 265浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《检查函数是否会调用另一个函数》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我愿意为我的 go 应用程序编写单元测试。

有一个函数会调用另一个函数,我该如何确认这个调用?

// the function which I wanna test
func big(t int) {

    bang(t * 6) // how to confirm this? 
}

我无法模拟 bang(),因为它不属于任何结构。


解决方案


简而言之,你没有。如果需要验证,您可以在它自己的单元测试中测试 bang 。然而,拥有没有返回值的函数是很棘手的——你只能测试它们与某些东西交互时的行为。

tl;dr:如果您需要测试行为,请尽可能简单地实际测试行为。特别是当您测试行为时,请彻底测试:它使您的代码更加健壮。

示例

一个“更好”(阅读:更具体和透明)的示例如下所示:

func hello(lang string) string {
    switch lang {
    case "de":
        return "hallo"
    case "es":
        return "hola"
    default:
        return "hello"
    }
}

func world(lang string) string {
    switch lang {
    case "de":
        return "welt"
    case "es":
        return "mundo"
    default:
        return "world"
    }
}

func greet(lang string) {
    var g string
    switch lang {
    case "de":
        g = fmt.sprintf("%s, %s!", hello("de"), world("de"))
    case "es":
        g = fmt.sprintf("¡%s, %s!", hello("es"), world("es"))
    default:
        g = fmt.sprintf("%s, %s!", hello(lang), world(lang))
    }

    fmt.println(g)
}

现在,函数 helloworld 很容易测试。以你好为例:

// testhello is heavily simplified for brevity.
func testhello(t *testing.t) {
    testcases := []struct {
        desc     string
        lang     string
        expected string
    }{
        {
            desc:     "german",
            lang:     "de",
            expected: "hallo",
        },
        {
            desc:     "spanish",
            lang:     "es",
            expected: "hola",
        },
        {
            desc:     "default",
            lang:     "en",
            expected: "hello",
        },
    }
    for _, tc := range testcases {
        t.run(tc.desc, func(t *testing.t) {
            if hello(tc.lang) != tc.expected {
                t.fail()
            }
        })
    }
}

world 重复此操作,您就会知道这两个函数的行为完全符合您的预期。

但是您将如何测试 greet 呢?它没有返回值,在当前形式下,确保写入内容的唯一方法是实际重定向 os.stdout 的输出。这可能就足够了,具体取决于您的用例,但它很麻烦且冗长。

因此,增强代码的可测试性更有意义:

// dsts is variadic to make it optional.
// so you can either call it as greet("en")
// or greet("en",whatever), with the added bonus
// that you can write to multiple destinations.
func greet(lang string, dsts ...io.writer) {

    var g string
    var out io.writer

    if dsts == nil {
        // if nothing is set, we want the default behavior.
        out = os.stdout
    } else if len(dsts) == 1 {
        // if only one dst is set for example for unit tests *wink* *wink*
        // we can write to it directly.
        out = dsts[0]
    } else {
        // in case multiple dsts are set, we utilize multiwriter
        out = io.multiwriter(dsts...)
    }

    switch lang {
    case "de":
        g = fmt.sprintf("%s, %s!", hello("de"), world("de"))
    case "es":
        g = fmt.sprintf("¡%s, %s!", hello("es"), world("es"))
    default:
        g = fmt.sprintf("%s, %s!", hello(lang), world(lang))
    }
    fmt.fprintln(out, g)
}

测试变得非常简单:

func TestGreet(t *testing.T) {

    buf := bytes.NewBuffer(nil)

    for _, lang := range []string{"de", "es", "unknown"} {
        greet(lang, buf)
        greeting := buf.String()

        // Of course, testing should be a bit more thorough
        if greeting == "" {
            t.Errorf("greeting for %s is empty!", lang)
        }

        if !strings.HasSuffix(greeting, "!\n") {
            t.Errorf("greeting '%s' for language '%s' has no exclamation mark or newline", greeting, lang)
        }
    }

}

更高级的技术(例如数据库连接)将是 use mocks

今天关于《检查函数是否会调用另一个函数》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

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