doukan5332 2015-11-14 23:12
浏览 56

Go-测试功能是否相等

So from what I've read, you can't test if a function is equal in Go, but I'm trying to solve a test-case issue, so any help in refactoring this would be helpful.

I have a constructor and I'm passing it some configuration values. Based on those configs, it assigns another constructor function to a member of the struct. Later, in a different method, it calls that new constructor. I did it this way because it made it easier to test the methods on the struct, since I could now create a test constructor and reassign the struct member to it, before calling the methods I was testing. Similar to the approach here: Mock functions in Go

Now though, I'm trying to write a test case on the struct constructor and I'm having a hard time figuring out how to test it.

Here's an example:

type requestBuilder func(portFlipArgs, PortFlipConfig) portFlipRequest

type portFlip struct {
    config  PortFlipConfig
    args    portFlipArgs
    builder requestBuilder
}

func newPortFlip(args portFlipArgs, config PortFlipConfig) (*portFlip, error) {
    p := &portFlip{args: args, config: config}
    if p.netType() == "sdn" {
        p.builder = newSDNRequest
    } else if p.netType() == "legacy" {
        p.builder = newLegacyRequest
    } else {
        return nil, fmt.Errorf("Invalid or nil netType: %s", p.netType())
    }
    return p, nil
}

The 'newSDNRequest' and 'newLegacyRequest' are the new constructors. I can't figure out how to test the newPortFlip method to make sure that's it assigning the right method to the 'builder' member, since you can't test function equality.

My only thought at this point is to have a 'builderType string' member, and just assign it to the name of the new constructor and then I could just test that. Something like:

func newPortFlip(args portFlipArgs, config PortFlipConfig) (*portFlip, error) {
    p := &portFlip{args: args, config: config}
    if p.netType() == "sdn" {
        p.builderType = "newSDNRequest"
        p.builder = newSDNRequest
    } else if p.netType() == "legacy" {
        p.builderType = "newLegacyRequest"
        p.builder = newLegacyRequest
    } else {
        return nil, fmt.Errorf("Invalid or nil netType: %s", p.netType())
    }
    return p, nil
}

But that seemed rather frivolous, so I figured I should seek a better way before I did that.

Thoughts?

  • 写回答

1条回答 默认 最新

  • dpd46554 2015-11-15 01:33
    关注

    Make portFlip an interface and have newPortFlip construct either an sdnPortFlip or a legacyPortFlip depending on the incoming type. In your test you can then check it's returning the correct concrete type using a type assertion.

    If you embed the common type into the SDN and legacy types then you can directly call those methods.

    type portFlip interface {
        build()
        ...
    }
    
    type portFlipCommon struct {
        config  PortFlipConfig
        args    portFlipArgs
    }
    
    type portFlipSdn struct {
        portFlipCommon
    }
    
    type portFlipLegacy struct {
        portFlipCommon
    }
    
    func (pf *portFlipCommon) netType() { ... }
    func (pf *portFlipSdn)    build() { ... }
    func (pf *portFlipLegacy) build() { ... }
    
    func newPortFlip(args portFlipArgs, config PortFlipConfig) (portFlip, error) {
        var pf portFlip
        p := &portFlipCommon{args: args, config: config}
        if p.netType() == "sdn" {
            // Either build directly or define build on the sdn type
            pf = &portFlipSdn{*p}
        } else if p.netType() == "legacy" {
            // Either build directly or define build on the legacy type
            pf = &portFlipLegacy{*p}
        } else {
            return nil, fmt.Errorf("Invalid or nil netType: %s", p.netType())
        }
        return pf, nil
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集