gpt4 book ai didi

unit-testing - 测试一个 go 闭包

转载 作者:数据小太阳 更新时间:2023-10-29 03:34:34 26 4
gpt4 key购买 nike

我有以下在 golang 中返回闭包的函数,任何想法/引用怎么可能为它编写测试?

  type (
OrderRepoInterface interface {
func save(msg Message) error
}

// OrderAggregation represents an event handler
EventHandler struct {
repo OrderRepoInterface // in main.go i pass a concrete repository here
}

VersionedEventHandler struct {
function func(msg *Message) error
}

Message struct {
version int
payload string
}

)

func (o *EventHandler) OnOrderWasCreated() VersionedEventHandler {
return func(msg *Message) error {
msg.version = 1
err := o.repo.save(msg)
return err
}
}

ps

这不是真正的代码,因为我使用了几个库,我用上面的代码起草了这个问题,希望它能告诉我我想要实现的目标,所以它可能无法编译

编辑

我所追求的是看到一些惯用的例子或想法,其中返回匿名函数的函数在 go 中被测试。

所以我不需要一个可行的解决方案。

最佳答案

有一个工作示例会很有帮助,所以我提供了一个。 对于这个简单的案例,可能只需要一个将版本号映射到处理订单保存的函数的字典就足够了。但我已尝试使用订单处理程序接口(interface)实现更接近您提供的内容。

对于测试,您需要某种日志来捕获副作用,以便您可以验证是否调用了正确的处理程序。为此,我添加了一个名为 eventLog 的全局字符串数组,可以附加到该数组。对于测试,您需要创建更多的 MultiVersionHandlers 和更多的测试消息。

您需要验证处理程序是否以您想象的方式响应测试消息,方法是调用处理程序的 save() 方法并将 eventLog 的内容与您预期的内容进行比较。此外,您将希望创建应该失败的消息。这些消息不会映射到处理程序支持的版本。然后验证是否返回了正确的错误值。我已经为你做了一些。

package main

import "fmt"

type MultiVersionHandler struct {
handlers map[int]OrderRepoInterface
}

type Message struct {
version int
payload string
}

type OrderRepoInterface interface {
save(Message) error
}

type OrderHandler struct {
saveHandler func(Message) error
}

// let's implement the OrderRepoInterface for a regular order handler
func (oh OrderHandler) save(msg Message) error {
return oh.saveHandler(msg)
}

// let's implement the OrderRepoInterface for a multi version order handler
func (mh MultiVersionHandler) save(msg Message) error {

if handler, ok := mh.handlers[msg.version]; ok {
return handler.save(msg)
}

return fmt.Errorf("doesn't support version %d, payload %q",
msg.version,
msg.payload)

}

// We will use eventLog capture simulations of a log of events which
// happen via our handlers. Useful for verification.
var eventLog = []string{}

func main() {

multiHandler := MakeMultiHandler()

msg1 := Message{payload: "make me a burger", version: 1}

msg2 := Message{payload: "make me a cake", version: 2}

msg3 := Message{payload: "make me a robot", version: 3}

// Create a message which has no handler.
// This message for version 4, should cause an error.
msg4 := Message{payload: "make me a planet", version: 4}

err := multiHandler.save(msg1)
err = multiHandler.save(msg2)
err = multiHandler.save(msg3)
err = multiHandler.save(msg4)
if err != nil {
fmt.Printf("Expecting an error: %q\n", err.Error())
// Expecting an error:
// "does not have a handler for version 4.
// Cannot process payload "make me a planet""

}

fmt.Printf("Event Log:%#v", eventLog)
// Event Log:[]string{
// "Amy will make me a burger",
// "Brandy will make me a cake",
// "Caleb will make me a robot"}



}

// This makes a multi version handler for an example.
// You should create more of these for testing different scenarios.
func MakeMultiHandler() OrderRepoInterface {

amy := OrderHandler{
saveHandler: func(msg Message) error {
action := "Amy will " + msg.payload
eventLog = append(eventLog, action)
return nil
},
}

brandy := OrderHandler{
saveHandler: func(msg Message) error {
action := "Brandy will " + msg.payload
eventLog = append(eventLog, action)
return nil
},
}

caleb := OrderHandler{
saveHandler: func(msg Message) error {
action := "Caleb will " + msg.payload
eventLog = append(eventLog, action)
return nil
},
}

multiHandler := MultiVersionHandler{
handlers: map[int]OrderRepoInterface{
1: amy, // amy should handle version 1 message
2: brandy, // brandy should handle version 2 message
3: caleb, // caleb should handle version 3 message
},
}

return multiHandler

}

关于unit-testing - 测试一个 go 闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50492110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com