我有以下函数在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
}
}
这不是真正的代码,因为我使用了几个库,我用上面的代码起草了这个问题,希望它能给出一个我想要实现的想法,所以它可能无法编译
我所追求的是看到一些惯用的例子或想法,其中返回一个匿名函数的函数在go中进行测试。
所以我不需要一个有效的解决方案。
答案 0 :(得分:1)
有一个有效的例子,所以我提供了一个。 对于这个简单的情况,仅仅有一个dict可以将版本号映射到处理订单保存的函数可能就足够了。但我尝试使用订单处理程序界面更接近您提供的内容。
对于测试,您需要某种日志来捕获副作用,以便您可以验证是否正在调用正确的处理程序。为此,我添加了一个名为eventLog的全局字符串数组,可以将其附加到。对于测试,您需要创建更多MultiVersionHandler和更多测试消息。
您需要通过调用处理程序上的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
}