Golang:替换函数单元测试

huangapple go评论77阅读模式
英文:

Golang: Replace function unit testing

问题

我正在使用Golang进行工作,目前正在使用Testify进行一些有趣的单元测试,我的文件如下所示:

type myStruct struct {
  field_1 string
}

func (self *myStruct) writeFirst() {
  //做一些事情
  //修改field_1
  self.writeSecond()
}

func (self *myStruct) writeSecond() {
  //做一些事情
}

在这种情况下,我正在测试writeFirst(),但我想替换writeSecond(),因为它使用了我不想使用的涉及互联网的http内容。

我认为使用第二个结构体并将myStruct设置为匿名字段将是解决方案,但它不起作用,因为我的第二个结构体和myStruct具有不同的上下文。

在这种情况下,我不能使用模拟,因为writeSecond是结构体的一个方法。

我的测试用例如下所示:

func TestWriteFirst(t *testing.T) {
   myStc := myStruct{}
   assert.Equal(t, "My response", myStc.field_1)
}

我想要的只是测试writeFirst而不传递给writeSecond()。

英文:

I'm working with Golang, and currently I'm doing some fun unit test with Testify, my file look like this

type myStruct struct {
  field_1 string

}
func (self *myStruct) writeFirst()  {
//doing something
//modify field_1
self.writeSecond()
}

func (self *myStruct) writeSecond() {
//doing something
}

In this case I'm testing writeFirst() but I'm trying to replace writeSecond() because it is using http stuff that I don't want to use because it access to internet.

I think that use a second struct and set myStruct as anonymous field will be the solution, but it's not working because me second struct and myStruct have a diferent context.

In this case I can't use mocks cause writeSecond is a method of the struct.

My test case looks like this:

func TestWriteFirst(t *testing.T) {
   myStc := myStruct{}
   assert.Equal(t,"My response", myStc.field_1)
}

All that I want is testing writeFirst without pass to writeSecond()

答案1

得分: 2

为了说明评论中提到的重构方式,你可以考虑只在一个实现了接口的实例上调用第二个函数:

type F2er interface {
    Func2()
}

type S struct{ _f2 F2er }

var s = &S{}

func (s *S) f2() F2er {
    if s._f2 == nil {
        return s
    }
    return s._f2
}

func (s *S) Func1() {
    fmt.Println("s.Func1")
    s.f2().Func2()
}

在这里,Func1s.f2() 上调用 Func2,而不是直接调用 s

  • 如果 s 中没有设置任何内容,s.f2() 返回... 它自己:s
  • 如果 s._f2 被任何其他实现了 Func2struct 替换,s.f2() 将返回该实例而不是自身。

在这个playground 脚本中可以看到一个完整的示例。

输出:

TestFunc1
s.Func1
s.Func2

TestFunc1bis
s.Func1
testS.Func2    <=== 不同的 Func2 调用
英文:

To illustrate the kind of refactoring mentioned by Not-a-Golfer in the comments, you could consider calling your second function only on an instance that is an interface:

type F2er interface {
	Func2()
}

type S struct{ _f2 F2er }

var s = &amp;S{}

func (s *S) f2() F2er {
	if s._f2 == nil {
		return s
	}
	return s._f2
}

func (s *S) Func1() {
	fmt.Println(&quot;s.Func1&quot;)
	s.f2().Func2()
}

Here: Func1 calls Func2 on s.f2(), not directly s.

  • If nothing has been set in s, s.f2() returns... itself: s
  • if s._f2 was replaced by any other struct which implements Func2, s.f2() returns that instance instead of itself.

See a complete example in this playground script.

Output:

TestFunc1
s.Func1
s.Func2

TestFunc1bis
s.Func1
testS.Func2    &lt;=== different Func2 call

huangapple
  • 本文由 发表于 2014年7月17日 05:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/24791040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定