Sid*_*ath 8 unit-testing mocking go
这是结构体及其方法的示例代码
type A struct {}
func (a *A) perfom(string){
...
...
..
}
Run Code Online (Sandbox Code Playgroud)
invoke()然后我想从包外部的函数调用该方法,示例代码
var s := A{}
func invoke(url string){
out := s.perfom(url)
...
...
}
Run Code Online (Sandbox Code Playgroud)
invoke我想通过模拟performA的方法来编写该函数的测试用例。
在java中,我们有mockito、jmock框架来存根方法调用。
go中有没有办法,我们可以模拟结构体的方法调用,而无需interfaces在源代码中引入?
要模拟方法调用,您需要模拟您的结构。
根据您提供的代码示例,我建议创建一个Performer实现您的Perform调用的接口。您的真实结构和模拟结构都将实现此接口。
我还建议将结构作为参数传递给调用函数,而不是使用全局变量。
这是一个例子:
type Performer interface {
perform()
}
type A struct {
}
func (a *A) perform() {
fmt.Println("real method")
}
type AMock struct {
}
func (a *AMock) perform () {
fmt.Println("mocked method")
}
func caller(p Performer) {
p.perform()
}
Run Code Online (Sandbox Code Playgroud)
在您的测试中,将模拟注入到您的invoke调用中。在您的真实代码中,将真实结构注入到您的invoke调用中。
使用像https://godoc.org/github.com/stretchr/testify/mock这样的库,您甚至可以非常轻松地验证您的方法是否使用正确的参数调用,调用正确的次数,并控制嘲笑的行为。
| 归档时间: |
|
| 查看次数: |
11047 次 |
| 最近记录: |